/** @brief Writes the current network configuration to non-volatile storage @private */ void gs_api_writeNetworkConfig(void) { // Try to save the network information //if (!GS_HAL_store_data(NETWORK_SAVE_ADDRESS, (void*) &apiNetworkConfig, // sizeof(apiNetworkConfig))) { GS_API_Printf("Network save failed"); //} }
/** @brief AtCmdLib calls this function for all incoming data @param cid Incoming data connection ID @param rxData Incoming data value */ void App_ProcessIncomingData(uint8_t cid, uint8_t rxData) { // Check for and call handler for incoming CID and Data if(cid < CID_COUNT && cidDataHandlers[cid]) cidDataHandlers[cid](cid, rxData); else GS_API_Printf("RX Data with no handler for cid %d\r\n", cid); }
void GS_API_CheckForData(void){ uint8_t rxData; /* Read one byte at a time - Use non-blocking call */ while (GS_HAL_recv(&rxData, 1, 0)) { /* Process the received data */ switch(AtLib_ReceiveDataProcess(rxData)){ case HOST_APP_MSG_ID_TCP_SERVER_CLIENT_CONNECTION: { uint8_t cidServerStr[] = " "; uint8_t cidClientStr[] = " "; uint8_t cidServer = GS_API_INVALID_CID; uint8_t cidClient = GS_API_INVALID_CID; if(AtLib_ParseTcpServerClientConnection((char *)cidServerStr, (char *)cidClientStr, tcpServerClientIp, tcpServerClientPort)){ cidServer = gs_api_parseCidStr(cidServerStr); cidClient = gs_api_parseCidStr(cidClientStr); gs_api_setCidDataHandler(cidClient, gs_api_getCidDataHandler(cidServer)); } GS_API_Printf("TCP Server Client Connection %d, %d, %s, %s\r\n", cidServer, cidClient, tcpServerClientIp, tcpServerClientPort); } break; case HOST_APP_MSG_ID_RESPONSE_TIMEOUT: case HOST_APP_MSG_ID_NONE: // Do nothing break; default: break; } } }
/** @brief Checks for a OK response to a command @private @param msg Message returned by command sent to Gainspan module @return true if command response was OK, false if not */ static bool gs_api_handle_cmd_resp(HOST_APP_MSG_ID_E msg) { if (msg != HOST_APP_MSG_ID_OK) { GS_API_Printf("CMD ERR %d", msg); return false; } else { return true; } }
/** @brief Parses command sent to TCP server This method will act upon the command sent to the TCP server, and send a response to the client that made the request to notify if the command was recognized or not. @private */ static void gs_example_tcp_handle_command(void){ switch(commandToHandle){ case TCP_NO_CMD: // Nothing to do break; case START_TCP_CLIENT: // Acknowledge command and start TCP client GS_API_SendTcpData(commandCID, TCP_COMMAND_ACK_STR, (sizeof(TCP_COMMAND_ACK_STR) - 1)); gs_example_start_tcp_client(value1, value2); break; case STOP_TCP_CLIENT: // Acknowledge command and stop TCP client GS_API_SendTcpData(commandCID, TCP_COMMAND_ACK_STR, (sizeof(TCP_COMMAND_ACK_STR) - 1)); GS_API_CloseConnection(tcpClientCID); tcpClientCID = GS_API_INVALID_CID; break; case TCP_FIRMWARE_UPGRADE: // Acknowledge command and attempt firmware update GS_API_SendTcpData(commandCID, TCP_COMMAND_ACK_STR, (sizeof(TCP_COMMAND_ACK_STR) - 1)); if(GS_API_UpdateFirmware((char*)value1, (char*)value2, (char*)value3, (char*)value4, (char*)value5)){ GS_API_Printf("Firmware Updated"); GS_Example_stop_tcp_task(); firmwareUpdated = true; } else { GS_API_Printf("Firmware Update Failed"); firmwareUpdated = true; } break; default: // Unknown command, send not ok and print the unrecognized command GS_API_Printf((char*)line); GS_API_SendTcpData(commandCID, TCP_COMMAND_NAK_STR, (sizeof(TCP_COMMAND_NAK_STR) - 1)); break; } // Clear the command so we don't act on it again commandToHandle = TCP_NO_CMD; }
/** @brief Starts a TCP client to connect to the specified server Tries to connect using TCP to the IP and port passed in. @private @param serverIp IP of TCP server to connect to @param serverPort port of TCP server to connect to */ static void gs_example_start_tcp_client(uint8_t* serverIp, uint8_t* serverPort){ // Check to see if we are already connected if(tcpClientCID != GS_API_INVALID_CID){ GS_API_Printf("TCP Client Already Started"); return; } // We're not connected, so lets create a TCP client connection tcpClientCID = GS_API_CreateTcpClientConnection((char*)serverIp, (char*)serverPort, gs_example_handle_tcp_client_data); // Now lets send data for the first time gs_example_send_tcp_client_data(); }
/** Public Method Implmentation **/ void GS_Example_start_tcp_task(void){ // Reset all variables memset(tcpServerClientCids, GS_API_INVALID_CID, sizeof(tcpServerClientCids)); tcpServerCID = GS_API_INVALID_CID; commandToHandle = TCP_NO_CMD; commandCID = GS_API_INVALID_CID; firmwareUpdated = false; // Create the TCP Server connection tcpServerCID = GS_API_CreateTcpServerConnection(TCP_SERVER_PORT, gs_example_handle_tcp_server_data); if(tcpServerCID != GS_API_INVALID_CID){ GS_API_Printf("TCP PORT: %s", TCP_SERVER_PORT); } }
/** @brief Reads the network configuration from non-volatile storage @private */ void gs_api_readNetworkConfig(void) { // Try to read the saved network information // if (!GS_HAL_read_data(NETWORK_SAVE_ADDRESS, (void*) &apiNetworkConfig, // sizeof(apiNetworkConfig))) { GS_API_Printf("No Saved Network"); // Set default data here memcpy(apiNetworkConfig.ssid, DEFAULT_SSID, sizeof(DEFAULT_SSID)); memcpy(apiNetworkConfig.channel, DEFAULT_CHANNEL, sizeof(DEFAULT_CHANNEL)); memcpy(apiNetworkConfig.dhcpEnabled, DEFAULT_DHCP_ENABLE, sizeof(DEFAULT_DHCP_ENABLE)); memcpy(apiNetworkConfig.security, DEFAULT_SECURITY, sizeof(DEFAULT_SECURITY)); //} }
/** @brief Sends periodic temperature using TCP client connection @private */ static void gs_example_send_tcp_client_data(){ static uint8_t temperatureStr[] = TCP_CLIENT_DATA_STR; // Check for a valid connection and timer interval if(tcpClientCID != GS_API_INVALID_CID && MSTimerDelta(tcpClientSentTime) > TCP_CLIENT_SEND_INTERVAL ){ // Format string with temperature sprintf((char*)temperatureStr, TCP_CLIENT_DATA_STR, GS_Example_GetTemperature()); // Try to send the temperature string if(!GS_API_SendTcpData(tcpClientCID, temperatureStr, sizeof(TCP_CLIENT_DATA_STR) - 1)){ // Sending failed, disable this connection ID tcpClientCID = GS_API_INVALID_CID; GS_API_Printf("Send TCP Data failed"); } // Reset sending interval tcpClientSentTime = MSTimerGet(); } }
void GS_API_PrintNetworkInfo(void){ GS_API_Printf("Network Info"); GS_API_Printf("SSID: %s", apiNetworkConfig.ssid); GS_API_Printf("Channel: %s", apiNetworkConfig.channel); }
bool GS_API_PollProvisioningResponse(void){ if(AtLib_ResponseHandleNoBlock() != HOST_APP_MSG_ID_NONE){ // Get the provisioning data HOST_APP_NETWORK_CONFIG_T networkConfig; memset(&networkConfig, 0, sizeof(networkConfig)); if (AtLib_ParseProvisionResponse(&networkConfig)) { // Print the Provisioning Network Options GS_API_Printf(networkConfig.ssid); GS_API_Printf(networkConfig.channel); GS_API_Printf(networkConfig.mode); GS_API_Printf(networkConfig.security); GS_API_Printf(networkConfig.dhcpEnabled); GS_API_Printf(networkConfig.passphrase); GS_API_Printf(networkConfig.wepKey); GS_API_Printf(networkConfig.wepId); GS_API_Printf(networkConfig.connType); GS_API_Printf(networkConfig.staticIp); GS_API_Printf(networkConfig.subnetMask); GS_API_Printf(networkConfig.gatewayIp); GS_API_Printf(networkConfig.autoDnsEnabled); GS_API_Printf(networkConfig.primaryDns); GS_API_Printf(networkConfig.secondaryDns); // Save the network configuration to non volatile storage memcpy(&apiNetworkConfig, &networkConfig, sizeof(apiNetworkConfig)); //gs_api_writeNetworkConfig(); return true; } } return false; }