DVBCore  20.3.0
DVBCore Documentation
ap_ipadd.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright © 2014 The DTVKit Open Software Foundation Ltd (www.dtvkit.org)
3  * Copyright © 2010 Ocean Blue Software Ltd
4  *
5  * This file is part of a DTVKit Software Component
6  * You are permitted to copy, modify or distribute this file subject to the terms
7  * of the DTVKit 1.0 Licence which can be found in licence.txt or at www.dtvkit.org
8  *
9  * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
10  * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
11  * OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * If you or your organisation is not a member of DTVKit then you have access
14  * to this source code outside of the terms of the licence agreement
15  * and you are expected to delete this and any associated files immediately.
16  * Further information on DTVKit, membership and terms can be found at www.dtvkit.org
17  *******************************************************************************/
26 //#define DEBUG_IP
27 
28 //---includes for this file-------------------------------------------------------------------------
29 // compiler library header files
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 
34 // Ocean Blue Software header files
35 #include "techtype.h" //generic data definitions
36 #include "dbgfuncs.h" //debug functions
37 
38 #include "stbhwos.h"
39 #include "stbhwip.h"
40 #include "stbhwnet.h"
41 #include "stbheap.h"
42 #include "app_nvm.h"
43 #include "ap_ipadd.h"
44 
45 //---constant definitions for this file-------------------------------------------------------------
46 #ifdef DEBUG_IP
47 #define DBG(X) STB_SPDebugWrite X
48 #else
49 #define DBG(X)
50 #endif
51 
52 #define IP_TASK_QUEUE_SIZE 10
53 #define IP_TASK_STACK 1024
54 #define IP_TASK_PRIO 10
55 #define IP_TASK_TIMEOUT 30000
56 #define IP_TASK_PERIOD 1000
57 
58 //---local typedefs, structs, enumerations for this file--------------------------------------------
59 typedef enum
60 {
61  IPTASK_DHCP,
62  IPTASK_STATIC
63 } E_IPTASK_COMMAND;
64 
65 typedef struct
66 {
67  E_IPTASK_COMMAND command;
68  void *semaphore;
70 
71 //---local (static) variable declarations for this file---------------------------------------------
72 // (internal variables declared static to make them local)
73 
74 
75 //---local function prototypes for this file--------------------------------------------------------
76 // (internal functions declared static to make them local)
77 static void ConnectByStaticIP(void);
78 static void ConnectByDhcp(BOOLEAN wait_for_completion);
79 static void StartIpTask(E_IPTASK_COMMAND command, BOOLEAN wait_for_completion);
80 static void IpTask(void *param);
81 
82 //--------------------------------------------------------------------------------------------------
83 // global function definitions
84 //--------------------------------------------------------------------------------------------------
85 
92 void AIP_ConnectToNetwork(BOOLEAN wait_for_completion)
93 {
94  E_NET_IF_TYPE net_if;
95  E_STB_IP_MODE ip_mode;
96  U8BIT *essid;
97  U8BIT *essid_password;
98  E_IPTASK_COMMAND cmd;
99  BOOLEAN connected;
100 
101  FUNCTION_START(AIP_ConnectToNetwork);
102 
103  net_if = (E_NET_IF_TYPE)APP_NvmRead(NET_IF_TYPE_NVM);
104  if (net_if != NET_IF_NONE)
105  {
106  if (net_if == NET_IF_WIRELESS)
107  {
108  /* Need to connect to the wireless network before setting/acquiring an IP address */
109  essid = APP_NvmReadString(ESSID_NVM);
110  essid_password = APP_NvmReadString(ESSID_PASSWORD_NVM);
111 
112  STB_NWSelectInterface(NW_WIRELESS);
113 
114  connected = STB_NWConnectToAccessPoint(essid, essid_password);
115  }
116  else
117  {
118  STB_NWSelectInterface(NW_WIRED);
119  connected = TRUE;
120  }
121 
122  if (connected)
123  {
124  ip_mode = (E_STB_IP_MODE) APP_NvmRead(IP_MODE_NVM);
125  if (ip_mode == IP_STATIC)
126  {
127  cmd = IPTASK_STATIC;
128  }
129  else
130  {
131  cmd = IPTASK_DHCP;
132  }
133 
134  StartIpTask(cmd, wait_for_completion);
135  }
136  }
137 
138  FUNCTION_FINISH(AIP_ConnectToNetwork);
139 }
140 
141 static void ConnectByStaticIP(void)
142 {
143  U8BIT ip_address[4];
144  U8BIT subnet_mask[4];
145  U8BIT gateway[4];
146  U8BIT dns[4];
147  U32BIT ip_address_32;
148  U32BIT subnet_mask_32;
149  U32BIT gateway_32;
150  U32BIT dns_32;
151 
152  FUNCTION_START(ConnectByStaticIP);
153 
154  // read ip from nvm and store in hw layer
155  ip_address_32 = APP_NvmRead(IP_ADDRESS_NVM);
156  subnet_mask_32 = APP_NvmRead(SUBNET_MASK_NVM);
157  gateway_32 = APP_NvmRead(GATEWAY_IP_NVM);
158  dns_32 = APP_NvmRead(DNS_SERVER_IP_NVM);
159 
160  ip_address[0] = (ip_address_32 & 0xFF000000) >> 24;
161  ip_address[1] = (ip_address_32 & 0x00FF0000) >> 16;
162  ip_address[2] = (ip_address_32 & 0x0000FF00) >> 8;
163  ip_address[3] = (ip_address_32 & 0x000000FF);
164 
165  subnet_mask[0] = (subnet_mask_32 & 0xFF000000) >> 24;
166  subnet_mask[1] = (subnet_mask_32 & 0x00FF0000) >> 16;
167  subnet_mask[2] = (subnet_mask_32 & 0x0000FF00) >> 8;
168  subnet_mask[3] = (subnet_mask_32 & 0x000000FF);
169 
170  gateway[0] = (gateway_32 & 0xFF000000) >> 24;
171  gateway[1] = (gateway_32 & 0x00FF0000) >> 16;
172  gateway[2] = (gateway_32 & 0x0000FF00) >> 8;
173  gateway[3] = (gateway_32 & 0x000000FF);
174 
175  dns[0] = (dns_32 & 0xFF000000) >> 24;
176  dns[1] = (dns_32 & 0x00FF0000) >> 16;
177  dns[2] = (dns_32 & 0x0000FF00) >> 8;
178  dns[3] = (dns_32 & 0x000000FF);
179 
180  STB_IPSetIPAddress(&ip_address[0]);
181  STB_IPSetSubnetMask(&subnet_mask[0]);
182  STB_IPSetGatewayIPAddress(&gateway[0]);
184 
185  FUNCTION_FINISH(ConnectByStaticIP);
186 }
187 
188 static void ConnectByDhcp(BOOLEAN wait_for_completion)
189 {
190  FUNCTION_START(ConnectByDhcp);
191  STB_IPGetIPByDhcp(wait_for_completion);
192  FUNCTION_FINISH(ConnectByDhcp);
193 }
194 
202 static void IpTask(void *param)
203 {
204  BOOLEAN wait;
205  void *semaphore;
206  E_IPTASK_COMMAND cmd;
207  U32BIT time_start;
208  E_NW_LINK_STATUS status;
209 
210  FUNCTION_START(IpTask);
211 
212  cmd = ((S_IPTASK_PARAMS *)param)->command;
213  time_start = STB_OSGetClockMilliseconds();
214 
215  /* Wait for the link to become active (or for the timeout) */
216  while (((status = STB_NWGetLinkStatus()) != NW_LINK_ACTIVE) && (STB_OSGetClockDiff(time_start) <= IP_TASK_TIMEOUT))
217  {
218  STB_OSTaskDelay(IP_TASK_PERIOD);
219  }
220 
221  if (status == NW_LINK_ACTIVE)
222  {
223  switch (cmd)
224  {
225  case IPTASK_DHCP:
226  {
227  semaphore = ((S_IPTASK_PARAMS *)param)->semaphore;
228  if (semaphore == NULL)
229  {
230  wait = FALSE;
231  }
232  else
233  {
234  wait = TRUE;
235  }
236  ConnectByDhcp(wait);
237  if (semaphore != NULL)
238  {
239  STB_OSSemaphoreSignal(semaphore);
240  }
241  break;
242  }
243 
244  case IPTASK_STATIC:
245  {
246  ConnectByStaticIP();
247  break;
248  }
249  }
250  }
251 #ifdef DEBUG_IP
252  else
253  {
254  DBG(("%s: Could not execute command because link is not after afer %d ms", __FUNCTION__, IP_TASK_TIMEOUT));
255  }
256 #endif
257 
258  STB_AppFreeMemory(param);
259 
260  FUNCTION_FINISH(IpTask);
261 }
262 
269 static void StartIpTask(E_IPTASK_COMMAND command, BOOLEAN wait_for_completion)
270 {
271  S_IPTASK_PARAMS *params;
272  void *semaphore = NULL;
273 
274  FUNCTION_START(StartIpTask);
275 
276  params = STB_AppGetMemory(sizeof(S_IPTASK_PARAMS));
277  if (params != NULL)
278  {
279  if (wait_for_completion)
280  {
281  semaphore = STB_OSCreateCountSemaphore(0);
282  params->semaphore = semaphore;
283  }
284  else
285  {
286  params->semaphore = NULL;
287  }
288  params->command = command;
289 
290  STB_OSCreateTask(IpTask, (void *)params, IP_TASK_STACK, IP_TASK_PRIO, (U8BIT *)"IpTask");
291 
292  if (wait_for_completion)
293  {
294  STB_OSSemaphoreWait(semaphore);
295  DBG(("%s: command=%s wait_for_completion=TRUE. Completed", __FUNCTION__,
296  (command == IPTASK_DHCP) ? "DHCP" : "Static"));
297  STB_OSDeleteSemaphore(semaphore);
298  }
299  }
300 #ifdef DEBUG_IP
301  else
302  {
303  DBG(("%s: failed allocating memory", __FUNCTION__));
304  }
305 #endif
306 
307  FUNCTION_FINISH(StartIpTask);
308 }
void STB_IPGetIPByDhcp(BOOLEAN wait_for_completion)
Cause the IP address to be set using DHCP.
Socket functions.
void AIP_ConnectToNetwork(BOOLEAN wait_for_completion)
Connect to network based on IP_MODE from NVM e.g. restore from IP NVM and connect to network...
Definition: ap_ipadd.c:92
BOOLEAN STB_NWSelectInterface(E_NW_INTERFACE iface)
Sets the network interface that will be used for all network or IP operations.
U32BIT STB_OSGetClockDiff(U32BIT timestamp)
Get Difference between Given Time and Current Time.
Header file for NVM data handling functions.
void * STB_AppGetMemory(U32BIT bytes)
Attempts to allocate memory from the application heap.
Definition: stbheap.c:651
void STB_OSSemaphoreSignal(void *semaphore)
Signal a Semaphore to Release it by decrementing its counter.
void * STB_OSCreateCountSemaphore(U32BIT value)
Create a counting semaphore.
U32BIT APP_NvmRead(E_NVM_ITEMS nvm_item)
Returns the current value for the given DVB setting.
Definition: app_nvm.c:562
E_NW_LINK_STATUS STB_NWGetLinkStatus(void)
Get the selected interface link status.
void STB_OSDeleteSemaphore(void *semaphore)
Delete a Semaphore.
void STB_OSSemaphoreWait(void *semaphore)
Wait on Semaphore Indefinity or Until Released.
Debug functions header file.
U8BIT * APP_NvmReadString(E_NVM_ITEMS nvm_item)
Returns pointer to current string for the given DVB setting.
Definition: app_nvm.c:605
Contains the initialise functions for IP.
void STB_IPSetIPAddress(const U8BIT *ip_addr)
Sets the IPv4 format IP address of default network connection.
void STB_IPSetGatewayIPAddress(const U8BIT *gateway_addr)
Sets the IPv4 format gateway IP address.
Header file - Function prototypes for operating system.
System Wide Global Technical Data Type Definitions.
void STB_IPSetDnsServerIPAddress(const U8BIT *dns_addr)
Sets the IPv4 format DNS server IP address.
void STB_AppFreeMemory(void *addr)
Releases previously allocated application heap memory.
Definition: stbheap.c:781
void STB_IPSetSubnetMask(const U8BIT *subnet_mask)
Sets the IPv4 format subnet mask of default network connection.
void * STB_OSCreateTask(void(*function)(void *), void *param, U32BIT stack, U8BIT priority, U8BIT *name)
Create a New Task to the calling process. Upon success, the created task runs on its own stack...
Header file - Function prototypes for heap memory.
macros and function prototypes for public use
void STB_OSTaskDelay(U16BIT timeout)
Delay Task for Specifed Time Period.
U32BIT STB_OSGetClockMilliseconds(void)
Get Current Computer Clock Time.
BOOLEAN STB_NWConnectToAccessPoint(U8BIT *essid, U8BIT *password)
Attempts to connect to the wireless network with the given SSID. If the network is open then &#39;passwor...