void log_print_(int level, int line, const char *func, const char *file, const char *msg, ...) { char _buffer[MAX_DEBUG_MESSAGE_LENGTH]; static char * levels[] = { "", "LOG ", "DEBUG", "WARN ", "ERROR", "PANIC", }; va_list args; va_start(args, msg); file = file ? strrchr(file,'/') + 1 : ""; int trunc = snprintf(_buffer, arraySize(_buffer), "%010lu:<%s> %s %s(%d):", GetSystem1MsTick(), levels[level], func, file, line); if (debug_output_) { debug_output_(_buffer); if (trunc > arraySize(_buffer)) { debug_output_("..."); } } trunc = vsnprintf(_buffer,arraySize(_buffer), msg, args); if (debug_output_) { debug_output_(_buffer); if (trunc > arraySize(_buffer)) { debug_output_("..."); } debug_output_("\r\n"); } }
/* * @brief This should block for a certain number of milliseconds and also execute spark_wlan_loop */ void delay(unsigned long ms) { #ifdef SPARK_WLAN_ENABLE volatile system_tick_t spark_loop_elapsed_millis = SPARK_LOOP_DELAY_MILLIS; spark_loop_total_millis += ms; #endif volatile system_tick_t last_millis = GetSystem1MsTick(); while (1) { KICK_WDT(); volatile system_tick_t current_millis = GetSystem1MsTick(); volatile long elapsed_millis = current_millis - last_millis; //Check for wrapping if (elapsed_millis < 0) { elapsed_millis = last_millis + current_millis; } if (elapsed_millis >= ms) { break; } #ifdef SPARK_WLAN_ENABLE if (!SPARK_WLAN_SETUP || SPARK_WLAN_SLEEP) { //Do not yield for SPARK_WLAN_Loop() } else if ((elapsed_millis >= spark_loop_elapsed_millis) || (spark_loop_total_millis >= SPARK_LOOP_DELAY_MILLIS)) { spark_loop_elapsed_millis = elapsed_millis + SPARK_LOOP_DELAY_MILLIS; //spark_loop_total_millis is reset to 0 in SPARK_WLAN_Loop() do { //Run once if the above condition passes SPARK_WLAN_Loop(); } while (SPARK_FLASH_UPDATE);//loop during OTA update } #endif } }
/* * @brief Should return the number of milliseconds since the processor started up. * This is useful for measuring the passage of time. * For now, let's not worry about what happens when this overflows (which should happen after 49 days). * At some point we'll have to figure that out, though. */ system_tick_t millis(void) { return GetSystem1MsTick(); }
//***************************************************************************** // //! HostFlowControlConsumeBuff //! //! @param sd socket descriptor //! //! @return 0 in case there are buffers available, //! -1 in case of bad socket //! -2 if there are no free buffers present (only when //! SEND_NON_BLOCKING is enabled) //! //! @brief if SEND_NON_BLOCKING not define - block until have free buffer //! becomes available, else return immediately with correct status //! regarding the buffers available. // //***************************************************************************** INT16 HostFlowControlConsumeBuff(INT16 sd) { #ifndef SEND_NON_BLOCKING /* wait in busy loop */ volatile system_tick_t start = GetSystem1MsTick(); do { // In case last transmission failed then we will return the last failure // reason here. // Note that the buffer will not be allocated in this case if (tSLInformation.slTransmitDataError != 0) { errno = tSLInformation.slTransmitDataError; tSLInformation.slTransmitDataError = 0; return errno; } if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd)) { return -1; } volatile system_tick_t now = GetSystem1MsTick(); volatile long elapsed = now - start; if (elapsed < 0) { // Did we wrap elapsed = start + now; // yes now } if (elapsed >= cc3000__event_timeout_ms) { ERROR("Timeout waiting on on buffers now %ld start %ld elapsed %ld cc3000__event_timeout_ms %ld",now,start,elapsed,cc3000__event_timeout_ms); return -1; } } while(0 == tSLInformation.usNumberOfFreeBuffers); tSLInformation.usNumberOfFreeBuffers--; return 0; #else // In case last transmission failed then we will return the last failure // reason here. // Note that the buffer will not be allocated in this case if (tSLInformation.slTransmitDataError != 0) { errno = tSLInformation.slTransmitDataError; tSLInformation.slTransmitDataError = 0; return errno; } if(SOCKET_STATUS_ACTIVE != get_socket_active_status(sd)) return -1; //If there are no available buffers, return -2. It is recommended to use // select or receive to see if there is any buffer occupied with received data // If so, call receive() to release the buffer. if(0 == tSLInformation.usNumberOfFreeBuffers) { return -2; } else { tSLInformation.usNumberOfFreeBuffers--; return 0; } #endif }
unsigned char * hci_event_handler(void *pRetParams, unsigned char *from, long *fromlen) { unsigned char *pucReceivedData, ucArgsize; unsigned short usLength; unsigned char *pucReceivedParams; unsigned short usReceivedEventOpcode = 0; unsigned long retValue32; unsigned char * RecvParams; unsigned char *RetParams; volatile system_tick_t start = GetSystem1MsTick(); while (1) { if (tSLInformation.usEventOrDataReceived == 0) { volatile system_tick_t now = GetSystem1MsTick(); volatile system_tick_t elapsed = now - start; if (elapsed < 0) { // Did we wrap elapsed = start + now; // yes now } if (cc3000__event_timeout_ms && (elapsed >= cc3000__event_timeout_ms)) { ERROR("Timeout now %ld start %ld elapsed %ld cc3000__event_timeout_ms %ld",now,start,elapsed,cc3000__event_timeout_ms); ERROR("Timeout waiting on tSLInformation.usRxEventOpcode 0x%04x",tSLInformation.usRxEventOpcode); // Timeout Return Error for requested Opcode // This sucks because callers should have initialized pucReceivedParams switch(tSLInformation.usRxEventOpcode) { default: INVALID_CASE(tSLInformation.usRxEventOpcode); break; case HCI_CMND_SIMPLE_LINK_START: case HCI_CMND_READ_BUFFER_SIZE: break; case HCI_CMND_WLAN_CONFIGURE_PATCH: case HCI_NETAPP_DHCP: case HCI_NETAPP_PING_SEND: case HCI_NETAPP_PING_STOP: case HCI_NETAPP_ARP_FLUSH: case HCI_NETAPP_SET_DEBUG_LEVEL: case HCI_NETAPP_SET_TIMERS: case HCI_EVNT_NVMEM_READ: case HCI_EVNT_NVMEM_CREATE_ENTRY: case HCI_CMND_NVMEM_WRITE_PATCH: case HCI_NETAPP_PING_REPORT: case HCI_EVNT_MDNS_ADVERTISE: case HCI_EVNT_READ_SP_VERSION: case HCI_EVNT_SELECT: *(unsigned char *)pRetParams = -1; break; case HCI_CMND_SETSOCKOPT: case HCI_CMND_WLAN_CONNECT: case HCI_CMND_WLAN_IOCTL_STATUSGET: case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE: case HCI_CMND_WLAN_IOCTL_DEL_PROFILE: case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY: case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX: case HCI_CMND_EVENT_MASK: case HCI_EVNT_WLAN_DISCONNECT: case HCI_EVNT_SOCKET: case HCI_EVNT_BIND: case HCI_CMND_LISTEN: case HCI_EVNT_CLOSE_SOCKET: case HCI_EVNT_CONNECT: case HCI_EVNT_NVMEM_WRITE: case HCI_EVNT_BSD_GETHOSTBYNAME: *(int32_t *)pRetParams = -1; break; case HCI_EVNT_RECV: case HCI_EVNT_RECVFROM: case HCI_EVNT_ACCEPT: case HCI_EVNT_SEND: case HCI_EVNT_SENDTO: case HCI_CMND_GETSOCKOPT: *(int32_t *)pRetParams = -1; pRetParams += sizeof(int32_t ); *(int32_t *)pRetParams = -1; break; case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS: *(int32_t *)pRetParams = 0; break; case HCI_NETAPP_IPCONFIG: memset(pRetParams,0,sizeof(tNetappIpconfigRetArgs)); break; } break; // Exit Loop } } else { pucReceivedData = (tSLInformation.pucReceivedData); if (*pucReceivedData == HCI_TYPE_EVNT) { // Event Received STREAM_TO_UINT16((char *)pucReceivedData, HCI_EVENT_OPCODE_OFFSET, usReceivedEventOpcode); pucReceivedParams = pucReceivedData + HCI_EVENT_HEADER_SIZE; RecvParams = pucReceivedParams; RetParams = pRetParams; // In case unsolicited event received - here the handling finished if (hci_unsol_event_handler((char *)pucReceivedData) == 0) { STREAM_TO_UINT8(pucReceivedData, HCI_DATA_LENGTH_OFFSET, usLength); switch(usReceivedEventOpcode) { case HCI_CMND_READ_BUFFER_SIZE: { STREAM_TO_UINT8((char *)pucReceivedParams, 0, tSLInformation.usNumberOfFreeBuffers); STREAM_TO_UINT16((char *)pucReceivedParams, 1, tSLInformation.usSlBufferLength); } break; case HCI_CMND_WLAN_CONFIGURE_PATCH: case HCI_NETAPP_DHCP: case HCI_NETAPP_PING_SEND: case HCI_NETAPP_PING_STOP: case HCI_NETAPP_ARP_FLUSH: case HCI_NETAPP_SET_DEBUG_LEVEL: case HCI_NETAPP_SET_TIMERS: case HCI_EVNT_NVMEM_READ: case HCI_EVNT_NVMEM_CREATE_ENTRY: case HCI_CMND_NVMEM_WRITE_PATCH: case HCI_NETAPP_PING_REPORT: case HCI_EVNT_MDNS_ADVERTISE: STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET,*(unsigned char *)pRetParams); break; case HCI_CMND_SETSOCKOPT: case HCI_CMND_WLAN_CONNECT: case HCI_CMND_WLAN_IOCTL_STATUSGET: case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE: case HCI_CMND_WLAN_IOCTL_DEL_PROFILE: case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY: case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP: case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX: case HCI_CMND_EVENT_MASK: case HCI_EVNT_WLAN_DISCONNECT: case HCI_EVNT_SOCKET: case HCI_EVNT_BIND: case HCI_CMND_LISTEN: case HCI_EVNT_CLOSE_SOCKET: case HCI_EVNT_CONNECT: case HCI_EVNT_NVMEM_WRITE: STREAM_TO_UINT32((char *)pucReceivedParams, 0, *(unsigned long *)pRetParams); break; case HCI_EVNT_READ_SP_VERSION: STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET, *(unsigned char *)pRetParams); pRetParams = ((char *)pRetParams) + 1; STREAM_TO_UINT32((char *)pucReceivedParams, 0, retValue32); UINT32_TO_STREAM((unsigned char *)pRetParams, retValue32); break; case HCI_EVNT_BSD_GETHOSTBYNAME: STREAM_TO_UINT32((char *)pucReceivedParams,GET_HOST_BY_NAME_RETVAL_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,GET_HOST_BY_NAME_ADDR_OFFSET,*(unsigned long *)pRetParams); break; case HCI_EVNT_ACCEPT: { STREAM_TO_UINT32((char *)pucReceivedParams,ACCEPT_SD_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,ACCEPT_RETURN_STATUS_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; //This argument returns in network order memcpy((unsigned char *)pRetParams, pucReceivedParams + ACCEPT_ADDRESS__OFFSET, sizeof(sockaddr)); break; } case HCI_EVNT_RECV: case HCI_EVNT_RECVFROM: { STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE__FLAGS__OFFSET,*(unsigned long *)pRetParams); if(((tBsdReadReturnParams *)pRetParams)->iNumberOfBytes == ERROR_SOCKET_INACTIVE) { set_socket_active_status(((tBsdReadReturnParams *)pRetParams)->iSocketDescriptor,SOCKET_STATUS_INACTIVE); } break; } case HCI_EVNT_SEND: case HCI_EVNT_SENDTO: { STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; break; } case HCI_EVNT_SELECT: { STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_STATUS_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_READFD_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_WRITEFD_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_EXFD_OFFSET,*(unsigned long *)pRetParams); break; } case HCI_CMND_GETSOCKOPT: STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET,((tBsdGetSockOptReturnParams *)pRetParams)->iStatus); //This argument returns in network order memcpy((unsigned char *)pRetParams, pucReceivedParams, 4); break; case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS: STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_TABlE_COUNT_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 4; STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 2; STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_FRAME_TIME_OFFSET,*(unsigned long *)pRetParams); pRetParams = ((char *)pRetParams) + 2; memcpy((unsigned char *)pRetParams, (char *)(pucReceivedParams + GET_SCAN_RESULTS_FRAME_TIME_OFFSET + 2), GET_SCAN_RESULTS_SSID_MAC_LENGTH); break; case HCI_CMND_SIMPLE_LINK_START: break; case HCI_NETAPP_IPCONFIG: //Read IP address STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); RecvParams += 4; //Read subnet STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); RecvParams += 4; //Read default GW STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); RecvParams += 4; //Read DHCP server STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); RecvParams += 4; //Read DNS server STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH); RecvParams += 4; //Read Mac address STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_MAC_LENGTH); RecvParams += 6; //Read SSID STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_SSID_LENGTH); } } if (usReceivedEventOpcode == tSLInformation.usRxEventOpcode) { tSLInformation.usRxEventOpcode = 0; tSLInformation.solicitedResponse = 0; } } else { if (tSLInformation.usRxDataPending == 0) { ERROR("type != HCI_TYPE_EVNT is (%d) usRxDataPending=%d usRxEventOpcode=%u usReceivedEventOpcode=%u", *pucReceivedData, tSLInformation.usRxDataPending, tSLInformation.usRxEventOpcode, usReceivedEventOpcode); } else { pucReceivedParams = pucReceivedData; STREAM_TO_UINT8((char *)pucReceivedData, HCI_PACKET_ARGSIZE_OFFSET, ucArgsize); STREAM_TO_UINT16((char *)pucReceivedData, HCI_PACKET_LENGTH_OFFSET, usLength); // Data received: note that the only case where from and from length // are not null is in recv from, so fill the args accordingly if (from) { STREAM_TO_UINT32((char *)(pucReceivedData + HCI_DATA_HEADER_SIZE), BSD_RECV_FROM_FROMLEN_OFFSET, *(unsigned long *)fromlen); memcpy(from, (pucReceivedData + HCI_DATA_HEADER_SIZE + BSD_RECV_FROM_FROM_OFFSET) ,*fromlen); } // Let's vet length long length = usLength - ucArgsize; if (length <= 0 || length > arraySize(wlan_rx_buffer)) { // Not sane length = -1; } else { memcpy(pRetParams, pucReceivedParams + HCI_DATA_HEADER_SIZE + ucArgsize, length); } // fixes the Nvram read not returning length if (fromlen) { *fromlen = length; } tSLInformation.usRxDataPending = 0; } } tSLInformation.usEventOrDataReceived = 0; SpiResumeSpi(); // Since we are going to TX - we need to handle this event after the // ResumeSPi since we need interrupts if ((*pucReceivedData == HCI_TYPE_EVNT) && (usReceivedEventOpcode == HCI_EVNT_PATCHES_REQ)) { hci_unsol_handle_patch_request((char *)pucReceivedData); } if ((tSLInformation.usRxEventOpcode == 0) && (tSLInformation.usRxDataPending == 0)) { break; } } } return NULL; }