/***************************************************************************** * * AT_response_check * * Checks for the expected response in our ring buffer. This is a destructive * read from the ring buffer - once read, the data is gone. * *****************************************************************************/ int AtModem_response_check(unsigned int checkTime, char *needle, char *termStr) { char linebuffer[100]; char * lineptr = linebuffer; unsigned char fullLine = 0; int result = -1; unsigned char line_length = 0; unsigned long startTime = MSTimerGet(); //WriteUILine(LCD_BLANK_LINE, LCD_AT_LINE); // a '0' in checkTime means read a long time if (!checkTime) checkTime = 0xFFFF; // keep checking until we either get the response we want or until // checkTime is elapsed while ((AT_REPLY_OK != result) && ((MSTimerGet() - startTime) < checkTime)) { line_length = AtModem_ReadLine(&fullLine, checkTime, lineptr, sizeof(linebuffer) - (lineptr - linebuffer), termStr); //check if we read a full line... if (fullLine) { line_length += lineptr - linebuffer; if (strnloc(linebuffer,needle,line_length)) { result = AT_REPLY_OK; } else //if we didn't find the response, but have a line... { if (strnloc(linebuffer,"ERROR",line_length)) // check for errors { result = AT_REPLY_ERROR; } else { if (strnloc(linebuffer,"OK",line_length)) // check for ?? some edge case??? { result = AT_REPLY_SGACT_NOT_SET; } } } //since we did not get AT_REPLY_OK yet, we continue to read the next line lineptr = linebuffer; line_length = 0; } else if ((lineptr + line_length - linebuffer) <= sizeof(linebuffer)) lineptr += line_length; // move lineptr in case we need to read again to get a full line else break; //we can't read any more } //linebuffer[line_length] = 0; //WriteUILine(linebuffer, LCD_AT_LINE); return result; }
/*---------------------------------------------------------------------------* * Routine: EEPROM_Write *---------------------------------------------------------------------------* * Description: * Write EEPROM * Inputs: * uint16_t add, char * pBuff * Outputs: * *---------------------------------------------------------------------------*/ void EEPROM_WriteStr(uint16_t addr, char *pdata) { /* Declare error flag */ uint8_t send[256]; I2C_Request r; uint16_t len = 2; send[0] = addr & 0xFF00; send[1] = addr & 0x00FF; while (*pdata != '\0') { send[len] = (uint8_t)*pdata; len++; pdata++; } uint32_t timeout = MSTimerGet(); r.iAddr = EEPROM_ADDR>>1; r.iSpeed = 100; /* kHz */ r.iWriteData = send; r.iWriteLength = len; r.iReadData = 0; r.iReadLength = 0; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} }
/*---------------------------------------------------------------------------* * Routine: Temperature_Get *---------------------------------------------------------------------------* * Description: * Read the value of the ADT7420 and return the temperature in Celcius. * Inputs: * void * Outputs: * uint16_t -- temperature with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ uint16_t Temperature_Get(void) { uint8_t target_reg; uint8_t target_data[2] = {0x00, 0x00}; uint16_t temp = 0; uint32_t timeout = MSTimerGet(); I2C_Request r; r.iAddr = ADT7420_ADDR>>1; r.iSpeed = 100; r.iWriteData = &target_reg; r.iWriteLength = 1; r.iReadData = target_data; r.iReadLength = 2; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} /* Convert the device measurement into a decimal number and insert into a temporary string to be displayed */ temp = (target_data[0] << 8) + target_data[1]; // temp = temp >> 3; return temp; }
/*---------------------------------------------------------------------------* * Routine: EEPROM_Seq_Read *---------------------------------------------------------------------------* * Description: * Read the value of the address and return the data . * Inputs: * void * Outputs: * uint16_t -- temperature with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ int16_t EEPROM_Seq_Read(uint16_t addr,uint8_t *pdata, uint16_t r_lenth) { uint8_t target_address[2]; uint32_t timeout = MSTimerGet(); I2C_Request r; int16_t result = 0; target_address[0] = addr & 0xFF00; target_address[1] = addr & 0x00FF; r.iAddr = EEPROM_ADDR >> 1; r.iSpeed = 100; r.iWriteData = target_address; r.iWriteLength = 2; r.iReadData = pdata; r.iReadLength = r_lenth; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} result = 1; return result; }
/***************************************************************************** * * UART_ReadBufferLine * * \param buffer: character buffer to tx; len: size of buffer; * * \return failure: -1; success: bytes rx'd * * \brief Reads characters out of the UART buffer into the caller buffer until * the buffer size is hit. Returns number of bytes * read into buffer. Populates fullLine with a '1' if a full line was * read. * *****************************************************************************/ int AtModem_ReadLineTimeOut(unsigned int checkTime, char *buffer, unsigned int bufSize) { char * lineptr = buffer; char * maxptr = buffer + bufSize; uint32_t startTime = MSTimerGet(); while (MSTimerDelta(startTime)< checkTime) { while (UART0_ReceiveByte((unsigned char *)lineptr)) { if ((lineptr + 1) > maxptr) // too many bytes to read into our line! { return lineptr - buffer; } else lineptr++; } } //if we read a partial line, but timed out, we still return how many //bytes we read just in case the caller wants to try to assemble the line //ConsolePrintf("timedout\r\n"); return lineptr - buffer; }
/*---------------------------------------------------------------------------* * Routine: Accelerometer_Get *---------------------------------------------------------------------------* * Description: * Read the value of the ADT7420 and return the LightSensor in Lux. * Inputs: * void * Outputs: * uint16_t -- LightSensor with 4 bits of fraction and 12 bits of * integer. *---------------------------------------------------------------------------*/ int16_t *Accelerometer_Get(void) { uint8_t target_reg, acc_axis; uint8_t target_data[2] = {0x00, 0x00}; uint32_t timeout = MSTimerGet(); I2C_Request r; //Accelerometer_Init(); for(acc_axis=0; acc_axis<3; acc_axis++) { target_reg = acc_reg_addr[acc_axis]; r.iAddr = ACCEL_ADDR>>1; r.iSpeed = 100; r.iWriteData = &target_reg; r.iWriteLength = 1; r.iReadData = target_data; r.iReadLength = 2; I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} /* Convert the device measurement into a decimal number and insert into a temporary string to be displayed */ gAccData[acc_axis] = (target_data[1] << 8) + target_data[0]; } return gAccData; }
/*---------------------------------------------------------------------------* * Routine: MSTimerDelay *---------------------------------------------------------------------------* * Description: * Routine to idle and delay a given number of milliseconds doing * nothing. * Inputs: * uint32_t ms -- Number of milliseconds to delay * Outputs: * void *---------------------------------------------------------------------------*/ void MSTimerDelay(uint32_t ms) { uint32_t start = MSTimerGet(); while (MSTimerDelta(start) < ms) { } }
/******************************************************************************* * Outline : EEPROM_Read * Description : This function writes the given contents to the * EEPROM, at the given location. * Argument : offset -- Offset byte from start of EEPROM * aData -- Pointer to bytes to write to EEPROM * aSize -- number of bytes to write to EEPROM * Return value : 0 = success, else failure *******************************************************************************/ uint8_t EEPROM_Read(uint16_t offset, uint8_t *aData, uint16_t aSize) { uint8_t writeData[2]; uint32_t timeout = MSTimerGet(); I2C_Request r; writeData[0] = (uint8_t)offset<<8; writeData[1] = (uint8_t)offset; r.iAddr = EEPROM_ADDR>>1; r.iSpeed = 100; r.iWriteData = writeData; r.iWriteLength = 2; r.iReadData = aData; r.iReadLength = aSize; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < EEPROM_TIMEOUT)) {} I2C_Read(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < EEPROM_TIMEOUT)) {} return 0; }
/*---------------------------------------------------------------------------*/ void TestGainSpan_ATCommands(void) { bool send = true; const uint8_t match[] = "AT\r\r\nOK\r\n\n\r\n"; uint8_t in[sizeof(match)]; uint8_t pos; uint8_t c; uint32_t start; DisplayLCD(LCD_LINE2, "GS ATCmd"); /* Reset the connection by flushing any waiting data */ start = MSTimerGet(); while (MSTimerDelta(start) < 500) { if (GainSpan_SPI_ReceiveByte(SPI_WIFI_CHANNEL, &c)) { Console_UART_SendByte(c); start = MSTimerGet(); } } start = MSTimerGet(); while (1) { /* Infitine loop */ if (send) { /* Do AT commands as fast as possible. */ GainSpan_SPI_SendData("AT\r\n", 4); send = false; pos = 0; start += 10; } if (GainSpan_SPI_ReceiveByte(SPI_WIFI_CHANNEL, &c)) { if (c != GAINSPAN_SPI_CHAR_IDLE) { in[pos++] = c; Console_UART_SendByte(c); if (pos == (sizeof(match) - 1)) { if (memcmp(match, in, sizeof(match) - 1) != 0) { Console_UART_SendByte('_'); ErrorCode(3); } send = true; } } } } }
// continously flushes the buffer for mSecs milli seconds void flushUart0RxBuffer(uint8_t mSecs) { uint8_t dummy; MSTimerInit(); while( MSTimerGet() <= mSecs) { UART0_ReceiveByte(&dummy); } }
/** * @brief Wait until we receive ack from master ble module * * After sending SPI message to master ble we should wait * for acknowledge or timeout. * * @return True if ack received */ static bool Sensors_Cfg_WaitAck(){ unsigned long long timeout; timeout = MSTimerGet(); Sensors_Cfg_ClrAck(); while (sensors_ack_rcv == false) { if (MSTimerDelta(timeout) > CFG_PASSKEY_WRITE_TIMEOUT) return false; } return sensors_ack_rcv; }
/** @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(); } }
// Flush the modems buffer void AtModem_FlushBuffer() { char linebuffer[200]; unsigned char fullLine = 1; unsigned char line_length = 0; unsigned long startTime = MSTimerGet(); AtModem_Write((unsigned char *)"\r", 1); while (fullLine) { line_length = AtModem_ReadLine( &fullLine, 2000, linebuffer + line_length, sizeof(linebuffer) - line_length, "\r\n"); } }
/*---------------------------------------------------------------------------* * Routine: LightSensor_Init *---------------------------------------------------------------------------* * Description: * Initialize the LightSensor driver. * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void LightSensor_Init(void) { /* Declare error flag */ uint8_t cmd[2] = { LIGHTSENSOR_CMD, 0x00 }; I2C_Request r; uint32_t timeout = MSTimerGet(); r.iAddr = LIGHTSENSOR_ADDR>>1; r.iSpeed = 100; /* kHz */ r.iWriteData = cmd; r.iWriteLength = 2; r.iReadData = 0; r.iReadLength = 0; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} }
/*---------------------------------------------------------------------------* * Routine: Temperature_Init *---------------------------------------------------------------------------* * Description: * Initialize the temperature ADT7420 driver. * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void Temperature_Init(void) { /* Declare error flag */ uint8_t cmd[2] = { ADT7420_CONFIG_REG, 0x00 }; I2C_Request r; uint32_t timeout = MSTimerGet(); r.iAddr = ADT7420_ADDR>>1; r.iSpeed = 100; /* kHz */ r.iWriteData = cmd; r.iWriteLength = 2; r.iReadData = 0; r.iReadLength = 0; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} }
/** * @brief Saves current message into first empty slot in buffer * * Finds first empty slot in message buffer and saves message. * Sets starting state of the message. * Returns message buffer index as message handler. * * @param MQTT message struct * @param Desired message state * * @return Message handler, 255 if buffer is full */ static unsigned char MQTT_Msg_StoreMessage(MQTT_User_Message_t* MyMessage, MQTT_Msg_State_t state){ unsigned char count = 255; // find first empty message slot in buffer for (count = 0; count < MQTT_API_MSG_BUFFER; count ++) { if (MQTT_Api_Messages[count].MQTT_MsgState == MQTT_MSG_STATE_EMPTY) break; } if (count == MQTT_API_MSG_BUFFER) return 255; // error no more space for messages // save message MQTT_Api_Messages[count].MQTT_MsgState = state; memcpy((void *) &MQTT_Api_Messages[count].MQTT_MyMessage, (const void *) MyMessage, sizeof(MQTT_User_Message_t)); MQTT_Api_Messages[count].TimeOfLastAction = MSTimerGet(); return count; }
/******************************************************************************* * Outline : EEPROM_Write * Description : This function writes the given contents to the * EEPROM, at the given location. * Argument : offset -- Offset byte from start of EEPROM * aData -- Pointer to bytes to write to EEPROM * aSize -- number of bytes to write to EEPROM * Return value : 0 = success, else failure *******************************************************************************/ uint8_t EEPROM_Write(uint16_t offset, uint8_t *aData, uint16_t aSize) { I2C_Request r; uint32_t timeout = MSTimerGet(); uint8_t writeData[EEPROM_BYTES_PER_WRITE+2]; uint16_t i, j, bytesToWrite; r.iAddr = EEPROM_ADDR>>1; r.iSpeed = 100; /* kHz */ // Write Data in groups of size defined by EEPROM_BYTES_PER_WRITE for(i=0; i<aSize; i+=EEPROM_BYTES_PER_WRITE) { // Data Address in the EEPROM to write to writeData[0] = (uint8_t)(i + offset)<<8; writeData[1] = (uint8_t)(i + offset); for(j=0; j<EEPROM_BYTES_PER_WRITE; j++) { writeData[2+j] = aData[i+j]; } if((aSize - i) < EEPROM_BYTES_PER_WRITE) bytesToWrite = aSize - i; else bytesToWrite = EEPROM_BYTES_PER_WRITE; r.iWriteData = writeData; r.iWriteLength = 2+bytesToWrite; r.iReadData = 0; r.iReadLength = 0; I2C_Start(); I2C_Write(&r, 0); MSTimerDelay(10); // Part requires a 5ms to process a data write while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < EEPROM_TIMEOUT)) {} } return 0; }
/*---------------------------------------------------------------------------* * Routine: Accelerometer_Init *---------------------------------------------------------------------------* * Description: * Initialize the Accelerometer driver. * Inputs: * void * Outputs: * void *---------------------------------------------------------------------------*/ void Accelerometer_Init(void) { I2C_Request r; uint32_t timeout; uint8_t acc_config_cnt; for(acc_config_cnt=0; acc_config_cnt<3; acc_config_cnt++) { timeout = MSTimerGet(); pTxData = (uint8_t *)acc_config[acc_config_cnt]; r.iAddr = ACCEL_ADDR>>1; r.iSpeed = 100; /* kHz */ r.iWriteData = pTxData; r.iWriteLength = 2; r.iReadData = 0; r.iReadLength = 0; I2C_Start(); I2C_Write(&r, 0); while ((I2C_IsBusy()) && (MSTimerDelta(timeout) < 10)) {} } }
/*---------------------------------------------------------------------------* * Routine: MSTimerDelta *---------------------------------------------------------------------------* * Description: * Calculate the current number of milliseconds expired since a given * start timer value. * Inputs: * uint32_t start -- Timer value at start of delta. * Outputs: * uint32_t -- Millisecond counter since given timer value. *---------------------------------------------------------------------------*/ uint32_t MSTimerDelta(uint32_t start) { return MSTimerGet() - start; }
/** * @brief Set time of last action to detect in progress timeout. * * @return void */ static void MQTT_Msg_SetLastTimeMsgInProgress(){ MQTT_MsgProcessBusy.LastAction = MSTimerGet(); }
int main(void) { AppMode_T AppMode; APP_STATE_E state=UPDATE_TEMPERATURE; char LCDString[30], temp_char[2]; uint16_t temp; float ftemp; HardwareSetup(); /************************initializa LCD module********************************/ SPI2_Init(); InitialiseLCD(); led_init(); MSTimerInit(); /* Default app mode */ AppMode = GAINSPAN_DEMO; /* If the CIK is exist, auto into the Exosite mode */ NVSettingsLoad(&GNV_Setting); /* Determine if SW1 & SW3 is pressed at power up to enter programming mode */ if (Switch1IsPressed() && Switch3IsPressed()) { AppMode = PROGRAM_MODE; } else if(Switch3IsPressed() && Switch2IsPressed()) { AppMode = EXOSITE_ERASE; } else if(Switch1IsPressed()) { AppMode = RUN_EXOSITE; } else if(Switch2IsPressed()) { AppMode = RUN_PROVISIONING; } else if(Switch3IsPressed()) { AppMode = RUN_OVER_AIR_DOWNLOAD; } if(AppMode == GAINSPAN_DEMO) { LCDDisplayLogo(); LCDSelectFont(FONT_SMALL); DisplayLCD(LCD_LINE3, "RL78G14 RDK V2.0"); DisplayLCD(LCD_LINE4, " Wi-Fi & Cloud "); DisplayLCD(LCD_LINE5, " demos by: "); DisplayLCD(LCD_LINE6, "Gainspan "); DisplayLCD(LCD_LINE7, "Exosite "); DisplayLCD(LCD_LINE8, "Future Designs, Inc"); MSTimerDelay(3500); ClearLCD(); DisplayLCD(LCD_LINE1, "Demo Modes: "); DisplayLCD(LCD_LINE2, "-RST no key: "); DisplayLCD(LCD_LINE3, " GS Web Server "); DisplayLCD(LCD_LINE4, "-RST + SW1: "); DisplayLCD(LCD_LINE5, " Exosite Cloud "); DisplayLCD(LCD_LINE6, "-RST + SW2: "); DisplayLCD(LCD_LINE7, " AP Provisioning "); DisplayLCD(LCD_LINE8, "-RST + SW3: OTA "); MSTimerDelay(3000); ClearLCD(); LCDSelectFont(FONT_LARGE); if(Exosite_GetCIK(NULL)) { AppMode = RUN_EXOSITE; } } DisplayLCD(LCD_LINE1, "Starting..."); /*****************************************************************************/ SPI_Init(GAINSPAN_SPI_RATE); /* Setup LCD SPI channel for Chip Select P10, active low, active per byte */ SPI_ChannelSetup(GAINSPAN_SPI_CHANNEL, false, true); GainSpan_SPI_Start(); PM15 &= ~(1 << 2); P15 &= ~(1 << 2); if(AppMode == PROGRAM_MODE) { App_ProgramMode(); } else if (AppMode == RUN_EXOSITE) { DisplayLCD(LCD_LINE1, " CLOUD DEMO "); Temperature_Init(); Potentiometer_Init(); App_Exosite(); } else if(AppMode == RUN_PROVISIONING) { App_WebProvisioning(); } else if(AppMode == RUN_OVER_AIR_DOWNLOAD) { App_OverTheAirProgrammingPushMetheod(); } else if (AppMode == EXOSITE_ERASE) { ClearLCD(); LCDSelectFont(FONT_SMALL); DisplayLCD(LCD_LINE3, "EEPROM ERASING ... "); MSTimerDelay(2000); Exosite_Init("renesas", "rl78g14", IF_WIFI, 1); DisplayLCD(LCD_LINE3, " "); DisplayLCD(LCD_LINE4, "Please reset device"); while(1); } else{ UART0_Start(GAINSPAN_CONSOLE_BAUD); // UART2_Start(GAINSPAN_UART_BAUD); Temperature_Init(); Potentiometer_Init(); // sprintf(LCDString, "RDK Demo %s", VERSION_TEXT); // DisplayLCD(LCD_LINE1, (const uint8_t *)LCDString); /* Before doing any tests or apps, startup the module */ /* and nonvolatile stettings */ App_Startup(); // Now connect to the system //App_Connect(&G_nvsettings.webprov); // App_PassThroughSPI(); /******************Start Processing Sensor data******************/ uint32_t start = MSTimerGet(); uint8_t c; Accelerometer_Init(); while(1) { // if (GainSpan_SPI_ReceiveByte(GAINSPAN_SPI_CHANNEL, &c)) if(App_Read(&c, 1, 0)) AtLibGs_ReceiveDataProcess(c); /* Timeout? */ if (MSTimerDelta(start) >= 100) // every 100 ms, read sensor data { led_task(); switch(state) { case UPDATE_TEMPERATURE: // Temperature sensor reading temp = Temperature_Get(); #if 0 // Get the temperature and show it on the LCD temp_char[0] = (int16_t)temp / 16; temp_char[1] = (int16_t)((temp & 0x000F) * 10) / 16; #endif temp_char[1] = (temp & 0xFF00)>>8; temp_char[0] = temp & 0xFF; ftemp = *(uint16_t *)temp_char; gTemp_F = ((ftemp/5)*9)/128 + 22; // Display the contents of lcd_buffer onto the debug LCD //sprintf((char *)LCDString, "TEMP: %d.%d C", temp_char[0], temp_char[1]); sprintf((char *)LCDString, "TEMP: %.1fF", gTemp_F); DisplayLCD(LCD_LINE6, (const uint8_t *)LCDString); state = UPDATE_LIGHT; break; case UPDATE_LIGHT: // Light sensor reading gAmbientLight = LightSensor_Get(); // Display the contents of lcd_buffer onto the debug LCD sprintf((char *)LCDString, "Light: %d ", gAmbientLight); DisplayLCD(LCD_LINE7, (const uint8_t *)LCDString); state = UPDATE_ACCELEROMETER; break; case UPDATE_ACCELEROMETER: // 3-axis accelerometer reading Accelerometer_Get(); sprintf((char *)LCDString, "x%2d y%2d z%2d", gAccData[0], gAccData[1], gAccData[2]); DisplayLCD(LCD_LINE8, (const uint8_t *)LCDString); state = UPDATE_TEMPERATURE; break; } start = MSTimerGet(); } } }
/*---------------------------------------------------------------------------*/ void App_TCPClientDemo(void) { ATLIBGS_MSG_ID_E rxMsgId = ATLIBGS_MSG_ID_NONE; static char content[512]; uint8_t cid = 0; static int16_t G_adc_int[2] = { 0, 0 }; static char G_temp_int[2] = { 0, 0 }; uint8_t remoteTcpSrvIp[20]; bool connected = false; // when connected to TCP server this is true uint32_t time = MSTimerGet(); ATLIBGS_TCPMessage msg; ATLIBGS_NetworkStatus networkStatus; AtLibGs_GetNetworkStatus(&networkStatus); AppTCPSetIPMenu(&networkStatus); sprintf((char*)remoteTcpSrvIp, "%d.%d.%d.%d", networkStatus.addr.ipv4[0], networkStatus.addr.ipv4[1], networkStatus.addr.ipv4[2], G_nvsettings.webprov.tcpIPClientHostIP); App_PrepareIncomingData(); while (1) { if (!AtLibGs_IsNodeAssociated()) { App_Connect(&G_nvsettings.webprov); connected = false; } else if (!connected) { DisplayLCD(LCD_LINE7, "Connecting"); // Start a TCP client rxMsgId = AtLibGs_TCPClientStart((char *)remoteTcpSrvIp, TCP_DEMO_REMOTE_TCP_SRVR_PORT, &cid); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, "No Connect!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } if (cid == ATLIBGS_INVALID_CID) { DisplayLCD(LCD_LINE7, "No CID!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } DisplayLCD(LCD_LINE7, ""); App_PrepareIncomingData(); connected = true; } else { App_TemperatureReadingUpdate(G_temp_int, true); App_PotentiometerUpdate(G_adc_int, true); // Look to see if there is a message if ((G_receivedCount) || (AtLibGs_WaitForTCPMessage(250) == ATLIBGS_MSG_ID_DATA_RX)) { // Got data! Its sitting in G_received, but in a <CID> <data> format // We need to send it back AtLibGs_ParseTCPData(G_received, G_receivedCount, &msg); // Prepare for the next batch of incoming data App_PrepareIncomingData(); // Copy the data out of the receive message (its sitting in G_recieved) memcpy(content, msg.message, msg.numBytes); // Now send this back over the TCP/IP connection rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, msg.numBytes); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, " Send Fail!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); continue; } } else if (MSTimerDelta(time) >= TCP_DEMO_UPDATE_INTERVAL) { time = MSTimerGet(); // send temp and ADC to last received TCP client every X seconds DisplayLCD(LCD_LINE7, " Sending"); sprintf(content, "Temp: %d.%d, Pot: %d.%d%%\r\n", G_temp_int[0], G_temp_int[1], G_adc_int[0], G_adc_int[1]); // send data to server rxMsgId = AtLibGs_SendTCPData(cid, (uint8_t *)content, strlen( content)); if (rxMsgId != ATLIBGS_MSG_ID_OK) { DisplayLCD(LCD_LINE7, " Send Fail!"); MSTimerDelay(2000); DisplayLCD(LCD_LINE7, ""); connected = false; continue; } // end if DisplayLCD(LCD_LINE7, ""); } // end else if } // end else } // end while }
/***************************************************************************** * * AtModem_GetMeid * * \param buffer: character buffer to for MEID; * * \return failure: 0; success: 1 * * \brief Reads characters out of the UART buffer into the caller buffer until * a CRLF is found or the buffer size is hit. Returns number of bytes * read into buffer. Populates fullLine with a '1' if a full line was * read. * *****************************************************************************/ uint8_t Novatel_GetMeid(char *buffer) { char linebuffer[200]; unsigned char fullLine = 1; unsigned char line_length = 0; unsigned char total_bytes = 0; uint8_t returnVal = 0; unsigned long startTime = MSTimerGet(); AtModem_Write((unsigned char *)"AT+CGSN\r", 8); //at+cgsn /************************************************* 45317471582021439:2159004912 0xa1000013c3e73f:0x80afccf0 *************************************************/ while (fullLine) { line_length = AtModem_ReadLine( &fullLine, 2000, linebuffer + line_length, sizeof(linebuffer) - line_length, "\r\n"); total_bytes += line_length; } // parsing this response assumes that the meid starts after the third // occurance of \n, begins with the ascii char 'x', and is terminated with // a ':' char uint8_t meidFound = 0; uint8_t delimeterCnt = 0; for (int i = 0; (i < total_bytes) & (!meidFound); i++) { // Find third occurance of \n if (linebuffer[i] == '\n') { delimeterCnt++; } // MEID starts at third occurance if (delimeterCnt == 3) { meidFound = 1; uint8_t meidCopied = 0; // advance i past '\n' and 'x' i += 2; // copy string until ':' found for (int j = 0; (j < (total_bytes - i)) & (!meidCopied); j++) { buffer[j] = linebuffer[i + j]; // check if next char is ':' if (linebuffer[i + j + 1] == ':') { meidCopied = 1; // add null terminator buffer[j + 1] = '\0'; returnVal = 1; } } } } /*************************************************/ return returnVal; }
/** * @brief Updates time of last action performed * * Used to determine timeouts for each message and action. * * @param Message handler * * @return void */ static void MQTT_Msg_UpdateLastActionTime(unsigned char handler){ MQTT_Api_Messages[handler].TimeOfLastAction = MSTimerGet(); }