//**************************************************************************** // //! \brief This function handles WLAN events //! //! \param[in] pSlWlanEvent is the event passed to the handler //! //! \return None // //**************************************************************************** void sl_WlanEvtHdlr(SlWlanEvent_t *pSlWlanEvent) { unsigned char ucQueueMsg = 0; switch(pSlWlanEvent->Event) { case SL_WLAN_CONNECT_EVENT: ucQueueMsg = (unsigned char)EVENT_CONNECTION; if(g_tConnection != 0) { osi_MsgQWrite(&g_tConnection, &ucQueueMsg, OSI_WAIT_FOREVER); } else { UART_PRINT("Error: sl_WlanEvtHdlr: Queue does not exist\n\r"); while(FOREVER); } UART_PRINT("C\n\r"); break; case SL_WLAN_DISCONNECT_EVENT: ucQueueMsg = (unsigned char)EVENT_DISCONNECTION; if(g_tConnection != 0) { osi_MsgQWrite(&g_tConnection, &ucQueueMsg, OSI_WAIT_FOREVER); } else { UART_PRINT("Error: sl_WlanEvtHdlr: Queue does not exist\n\r"); while(FOREVER); } UART_PRINT("D\n\r"); break; default: break; } }
//***************************************************************************** // //! \brief Interrupt Handler to Send Email upon Press of Push Button (S3) //! //! \param none //! //! \return void //! \note // //***************************************************************************** void SendEmailInterruptHandler() { tPushButtonMsg sMsg; sMsg.pEntry = (P_OSI_SPAWN_ENTRY)&PushButtonMailSend; sMsg.pValue = NULL; osi_MsgQWrite(&g_PBQueue,&sMsg,OSI_NO_WAIT); }
//**************************************************************************** // //! \brief This function handles events for IP address acquisition via DHCP //! indication //! //! \param[in] pNetAppEvent is the event passed to the handler //! //! \return None // //**************************************************************************** void sl_NetAppEvtHdlr(SlNetAppEvent_t *pNetAppEvent) { unsigned char ucQueueMsg = 0; switch(pNetAppEvent->Event) { case SL_NETAPP_IPV4_IPACQUIRED_EVENT: case SL_NETAPP_IPV6_IPACQUIRED_EVENT: g_ulIpAddr = pNetAppEvent->EventData.ipAcquiredV4.ip; ucQueueMsg = (unsigned char)EVENT_IP_ACQUIRED; if(g_tConnection != 0) { osi_MsgQWrite(&g_tConnection, &ucQueueMsg, OSI_WAIT_FOREVER); } else { UART_PRINT("Error: sl_NetAppEvtHdlr: Queue does not exist\n\r"); while(FOREVER); } UART_PRINT("IP: "); PrintIPAddr(g_ulIpAddr); UART_PRINT("\n\r"); break; default: break; } }
//***************************************************************************** // //! \brief Interrupt Handler to Start Smart Config upon Press of Push Button (S2) //! //! \param none //! //! \return void //! \note //! \warning // //***************************************************************************** void SmartConfigInterruptHandler() { tPushButtonMsg sMsg; sMsg.pEntry = (P_OSI_SPAWN_ENTRY)&SmartConfigTask; sMsg.pValue = NULL; osi_MsgQWrite(&g_PBQueue,&sMsg,OSI_NO_WAIT); }
//***************************************************************************** // //! The interrupt handler for the watchdog timer //! //! \param None //! //! \return None // //***************************************************************************** void WatchdogIntHandler(void) { // // If we have been told to stop feeding the watchdog, return immediately // without clearing the interrupt. This will cause the system to reset // next time the watchdog interrupt fires. // if(!g_ucFeedWatchdog) { return; } unsigned char ucQueueMsg = 0; if(g_ucWdogCount < CONNECTION_RETRIES) { g_ucWdogCount++; ucQueueMsg = (unsigned char)WDOG_EXPIRED; MAP_WatchdogIntClear(WDT_BASE); } else { g_ucFeedWatchdog = 0; ucQueueMsg = (unsigned char)CONNECTION_FAILED; MAP_WatchdogIntClear(WDT_BASE); } if(g_tConnection != 0) { osi_MsgQWrite(&g_tConnection, &ucQueueMsg, OSI_NO_WAIT); } else { UART_PRINT("Error: WatchdogIntHandler: Queue does not exist\n\r"); } }
void invoke_cb(cb_t cb, void *arg) { assert(cb & CB_ADDR_MASK == 0); struct sj_event e; e.type = INVOKE_CB_EVENT; e.cb = (unsigned) cb; e.data = arg; osi_MsgQWrite(&s_main_queue, &e, OSI_WAIT_FOREVER); }
//***************************************************************************** // //! DMA SPI interrupt handler //! //! \param None //! //! This function //! 1. Invoked when SPI Transaction Completes //! //! \return None. // //***************************************************************************** void DmaSpiSwIntHandler() { SPIIntClear(LSPI_BASE,SPI_INT_EOW); SPICSDisable(LSPI_BASE); #if defined(SL_PLATFORM_MULTI_THREADED) osi_MsgQWrite(&DMAMsgQ,g_cDummy,OSI_NO_WAIT); #else g_cDummy = 0x1; #endif }
//***************************************************************************** // //! \brief callback function for gpio interrupt handler //! //! \param gpio_num is the gpio number which has triggered the interrupt //! //! \return 0 // //***************************************************************************** int gpio_intr_hndlr(int gpio_num) { unsigned char queue_msg = 2; if(GPIO_SRC_WKUP == gpio_num) { osi_MsgQWrite(&g_tWkupSignalQueue, &queue_msg, OSI_NO_WAIT); } return 0; }
//**************************************************************************** // //! Push Button Handler3(GPIOS3). Press push button3 (GPIOSW3) Whenever user //! wants to publish a message. Write message into message queue signaling the //! event publish messages //! //! \param none //! //! return none // //**************************************************************************** void pushButtonInterruptHandler3() { event_msg msg; msg.event = PUSH_BUTTON_SW3_PRESSED; msg.hndl = NULL; // // write message indicating exit from sending loop // osi_MsgQWrite(&g_PBQueue,&msg,OSI_NO_WAIT); }
//**************************************************************************** // //! Push Button Handler1(GPIOS2). Press push button2 (GPIOSW2) Whenever user //! wants to publish a message. Write message into message queue signaling the //! event publish messages //! //! \param none //! //! return none // //**************************************************************************** void pushButtonInterruptHandler2() { event_msg msg; msg.event = PUSH_BUTTON_SW2_PRESSED; msg.hndl = NULL; // // write message indicating publish message // osi_MsgQWrite(&g_PBQueue,&msg,OSI_NO_WAIT); }
//***************************************************************************** // //! Control for Wifi Audio Player Speaker //! //! \param None //! //! \return None // //***************************************************************************** static void SpeakerStartStopControl() { long lRetVal = -1; tTxMsg sMsg; sMsg.pEntry = &SpeakerControl; sMsg.pValue = NULL; lRetVal = osi_MsgQWrite(&g_ControlMsgQueue,&sMsg,OSI_NO_WAIT); if(lRetVal < 0) { UART_PRINT("Unable to write to message queue\n\r"); LOOP_FOREVER(); } }
void sj_invoke_cb(struct v7 *v7, v7_val_t func, v7_val_t this_obj, v7_val_t args) { struct prompt_event pe; struct v7_invoke_event_data *ied = calloc(1, sizeof(*ied)); ied->func = func; ied->this_obj = this_obj; ied->args = args; v7_own(v7, &ied->func); v7_own(v7, &ied->this_obj); v7_own(v7, &ied->args); pe.type = V7_INVOKE_EVENT; pe.data = ied; osi_MsgQWrite(&s_v7_q, &pe, OSI_WAIT_FOREVER); }
//**************************************************************************** // //! callback event in case of MQTT disconnection //! //! \param app_hndl is the handle for the disconnected connection //! //! return none // //**************************************************************************** static void sl_MqttDisconnect(void *app_hndl) { connect_config *local_con_conf; event_msg msg; local_con_conf = app_hndl; msg.hndl = app_hndl; msg.event = BROKER_DISCONNECTION; UART_PRINT("disconnect from broker %s\r\n", (local_con_conf->broker_config).server_info.server_addr); local_con_conf->is_connected = false; // // write message indicating publish message // osi_MsgQWrite(&g_PBQueue,&msg,OSI_NO_WAIT); }
void ControlServer(void *pvParameters) { char ServerBuffer[CONFIG_SERVER_BUFFER]; char MsgBuffer[100]; char *pMsgBuffer; SlSockAddrIn_t sAddr; SlSockAddrIn_t sLocalAddr; int32_t i32SockID; int32_t i32NewSockID; int32_t i32DataSize; int32_t i32NonBlocking = 1; SlSocklen_t i32AddrSize; int32_t retval; pMsgBuffer = &MsgBuffer[0]; InitVariables(); retval = ResetSimpleLink(); if (retval < 0) while (1) ; WlanConnect(); sprintf(MsgBuffer, "Connectado a rede: %s\n\r" "IP: %d.%d.%d.%d\n\r" "Gateway: %d.%d.%d.%d\n\r", SL_IPV4_BYTE(g_sSLCon.DeviceIP, 3), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 2), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 1), SL_IPV4_BYTE(g_sSLCon.DeviceIP, 0), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 3), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 2), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 1), SL_IPV4_BYTE(g_sSLCon.GatewayIP, 0)); osi_MsgQWrite(&g_sUartQuee, &pMsgBuffer, OSI_NO_WAIT); sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short) g_sSLCon.PortNumber); sLocalAddr.sin_addr.s_addr = 0; i32SockID = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, 0); sl_Bind(i32SockID, (SlSockAddr_t *) &sLocalAddr, sizeof(SlSockAddrIn_t)); sl_Listen(i32SockID, 0); sl_SetSockOpt(i32SockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &i32NonBlocking, sizeof(i32NonBlocking)); while (1) { i32NewSockID = SL_EAGAIN; while (i32NewSockID < 0) { i32NewSockID = sl_Accept(i32SockID, (struct SlSockAddr_t *) &sAddr, (SlSocklen_t*) &i32AddrSize); if (i32NewSockID == SL_EAGAIN) { osi_Sleep(100); } else if (i32NewSockID < 0) { while (1) { } } } i32DataSize = sl_Recv(i32NewSockID, ServerBuffer, CONFIG_SERVER_BUFFER, 0); if (strcmp(ServerBuffer, "Led On") == 0) { Led_Green(LED_ON); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Led Off") == 0) { Led_Green(LED_OFF); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Lamp On") == 0) { MAP_GPIOPinWrite(GPIOA0_BASE, GPIO_PIN_6, GPIO_PIN_6); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } else if (strcmp(ServerBuffer, "Lamp Off") == 0) { MAP_GPIOPinWrite(GPIOA0_BASE, GPIO_PIN_6, 0); strcpy(ServerBuffer, "OK"); i32DataSize = 3; } sl_Send(i32NewSockID, ServerBuffer, i32DataSize, 0); sl_Close(i32NewSockID); } }
void postButton3Msg(void) { ApplicationMessage msg; msg.handler = &onButton3Pressed; msg.params = NULL; osi_MsgQWrite(&g_ApplicationMessageQueue, &msg, OSI_NO_WAIT); }
static void uart_int() { int c = UARTCharGet(CONSOLE_UART); struct prompt_event pe = {.type = PROMPT_CHAR_EVENT, .data = (void *) c}; osi_MsgQWrite(&s_v7_q, &pe, OSI_NO_WAIT); MAP_UARTIntClear(CONSOLE_UART, UART_INT_RX); } void sj_prompt_init_hal(struct v7 *v7) { (void) v7; } static void v7_task(void *arg) { struct v7 *v7 = s_v7; printf("\n\nSmart.JS for CC3200\n"); osi_MsgQCreate(&s_v7_q, "V7", sizeof(struct prompt_event), 32 /* len */); osi_InterruptRegister(CONSOLE_UART_INT, uart_int, INT_PRIORITY_LVL_1); MAP_UARTIntEnable(CONSOLE_UART, UART_INT_RX); sl_Start(NULL, NULL, NULL); v7 = s_v7 = init_v7(&v7); sj_init_timers(v7); sj_init_v7_ext(v7); init_wifi(v7); if (init_fs(v7) != 0) { fprintf(stderr, "FS initialization failed.\n"); } mongoose_init(); sj_init_http(v7); init_i2cjs(v7); /* Common config infrastructure. Mongoose & v7 must be initialized. */ init_device(v7); v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { fprintf(stderr, "Error: "); v7_fprint(stderr, v7, res); } sj_prompt_init(v7); while (1) { struct prompt_event pe; mongoose_poll(MONGOOSE_POLL_LENGTH_MS); if (osi_MsgQRead(&s_v7_q, &pe, V7_POLL_LENGTH_MS) != OSI_OK) continue; switch (pe.type) { case PROMPT_CHAR_EVENT: { sj_prompt_process_char((char) ((int) pe.data)); break; } case V7_INVOKE_EVENT: { struct v7_invoke_event_data *ied = (struct v7_invoke_event_data *) pe.data; _sj_invoke_cb(v7, ied->func, ied->this_obj, ied->args); v7_disown(v7, &ied->args); v7_disown(v7, &ied->this_obj); v7_disown(v7, &ied->func); free(ied); break; } } } } /* Int vector table, defined in startup_gcc.c */ extern void (*const g_pfnVectors[])(void); void device_reboot(void) { sj_system_restart(); } int main() { MAP_IntVTableBaseSet((unsigned long) &g_pfnVectors[0]); MAP_IntEnable(FAULT_SYSTICK); MAP_IntMasterEnable(); PRCMCC3200MCUInit(); cc3200_leds_init(); /* Console UART init. */ MAP_PRCMPeripheralClkEnable(CONSOLE_UART_PERIPH, PRCM_RUN_MODE_CLK); MAP_PinTypeUART(PIN_55, PIN_MODE_3); /* PIN_55 -> UART0_TX */ MAP_PinTypeUART(PIN_57, PIN_MODE_3); /* PIN_57 -> UART0_RX */ MAP_UARTConfigSetExpClk( CONSOLE_UART, MAP_PRCMPeripheralClockGet(CONSOLE_UART_PERIPH), CONSOLE_BAUD_RATE, (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE | UART_CONFIG_PAR_NONE)); MAP_UARTFIFODisable(CONSOLE_UART); setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); VStartSimpleLinkSpawnTask(8); osi_TaskCreate(v7_task, (const signed char *) "v7", V7_STACK_SIZE + 256, NULL, 3, NULL); osi_TaskCreate(blinkenlights_task, (const signed char *) "blink", 256, NULL, 9, NULL); osi_start(); return 0; }
static void uart_int() { struct sj_event e = {.type = PROMPT_CHAR_EVENT, .data = NULL}; MAP_UARTIntClear(CONSOLE_UART, UART_INT_RX | UART_INT_RT); MAP_UARTIntDisable(CONSOLE_UART, UART_INT_RX | UART_INT_RT); osi_MsgQWrite(&s_main_queue, &e, OSI_NO_WAIT); }
void mg_run_in_task(void (*cb)(struct mg_mgr *mgr, void *arg), void *cb_arg) { struct mg_q_msg msg = {MG_Q_MSG_CB, cb, cb_arg}; osi_MsgQWrite(&s_mg_q, &msg, OSI_NO_WAIT); }
//***************************************************************************** // //! \brief Task Created by main fucntion.This task starts simpleink, set NWP //! power policy, connects to an AP. Give Signal to the other task about //! the connection.wait for the message form the interrupt handlers and //! the other task. Accordingly print the wake up cause from the low //! power modes. //! //! \param pvParameters is a general void pointer (not used here). //! //! \return none // //***************************************************************************** void TimerGPIOTask(void *pvParameters) { cc_hndl tTimerHndl = NULL; cc_hndl tGPIOHndl = NULL; unsigned char ucQueueMsg = 0; unsigned char ucSyncMsg = 0; int iRetVal = 0; // // Displays the Application Banner // DisplayBanner(); // // creating the queue for signalling about connection events // iRetVal = osi_MsgQCreate(&g_tConnection, NULL, sizeof( unsigned char ), 3); if (iRetVal < 0) { UART_PRINT("unable to create the msg queue\n\r"); LOOP_FOREVER(); } // // starting the simplelink // iRetVal = sl_Start(NULL, NULL, NULL); if (iRetVal < 0) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(); } // // Swtich to STA mode if device is not // SwitchToStaMode(iRetVal); // // Set the power management policy of NWP // iRetVal = sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0); if (iRetVal < 0) { UART_PRINT("unable to configure network power policy\n\r"); LOOP_FOREVER(); } // // connecting to the Access Point // if(-1 == WlanConnect()) { sl_Stop(SL_STOP_TIMEOUT); UART_PRINT("Connection to AP failed\n\r"); } else { UART_PRINT("Connected to AP\n\r"); // //signal the other task about the sl start and connection to the AP // iRetVal = osi_MsgQWrite(&g_tConnectionFlag, &ucSyncMsg, OSI_WAIT_FOREVER); if (iRetVal < 0) { UART_PRINT("unable to create the msg queue\n\r"); LOOP_FOREVER(); } } // // Queue management related configurations // iRetVal = osi_MsgQCreate(&g_tWkupSignalQueue, NULL, sizeof( unsigned char ), 10); if (iRetVal < 0) { UART_PRINT("unable to create the msg queue\n\r"); LOOP_FOREVER(); } // // setting Timer as one of the wakeup source // tTimerHndl = SetTimerAsWkUp(); // // setting some GPIO as one of the wakeup source // tGPIOHndl = SetGPIOAsWkUp(); /* handles, if required, can be used to stop the timer, but not used here*/ UNUSED(tTimerHndl); UNUSED(tGPIOHndl); // // setting Apps power policy // lp3p0_setup_power_policy(POWER_POLICY_STANDBY); while(FOREVER) { // // waits for the message from the various interrupt handlers(GPIO, // Timer) and the UDPServerTask. // osi_MsgQRead(&g_tWkupSignalQueue, &ucQueueMsg, OSI_WAIT_FOREVER); switch(ucQueueMsg){ case 1: UART_PRINT("timer\n\r"); break; case 2: UART_PRINT("GPIO\n\r"); break; case 3: UART_PRINT("host irq\n\r"); break; default: UART_PRINT("invalid msg\n\r"); break; } } }
//***************************************************************************** // //! \brief callback function for timer interrupt handler //! //! \param vParam is a general void pointer (not used here) //! //! \return None // //***************************************************************************** void TimerCallback(void *vParam) { unsigned char ucQueueMsg = 1; osi_MsgQWrite(&g_tWkupSignalQueue, &ucQueueMsg, OSI_NO_WAIT); return; }
bool invoke_cb(cb_t cb, void *arg) { struct miot_event e = {.cb = cb, .arg = arg}; return (osi_MsgQWrite(&s_main_queue, &e, OSI_NO_WAIT) == OSI_OK); }
//***************************************************************************** // //! \brief Task Created by main fucntion. This task creates a udp server and //! wait for packets. Upon receiving the packet, signals the other task. //! //! \param pvParameters is a general void pointer (not used here). //! //! \return none // //***************************************************************************** void UDPServerTask(void *pvParameters) { unsigned char ucSyncMsg; unsigned char ucQueueMsg = 3; int iSockDesc = 0; int iRetVal = 0; sockaddr_in sLocalAddr; sockaddr_in sClientAddr; unsigned int iAddrSize = 0; // // waiting for the other task to start simplelink and connection to the AP // osi_MsgQRead(&g_tConnectionFlag, &ucSyncMsg, OSI_WAIT_FOREVER); osi_MsgQDelete(&g_tConnectionFlag); // // configure the Server // sLocalAddr.sin_family = SL_AF_INET; sLocalAddr.sin_port = sl_Htons((unsigned short)APP_UDP_PORT); sLocalAddr.sin_addr.s_addr = 0; iAddrSize = sizeof(sockaddr_in); // // creating a UDP socket // iSockDesc = sl_Socket(SL_AF_INET,SL_SOCK_DGRAM, 0); if(iSockDesc < 0) { UART_PRINT("sock error\n\r"); LOOP_FOREVER(); } // // binding the socket // iRetVal = sl_Bind(iSockDesc, (SlSockAddr_t *)&sLocalAddr, iAddrSize); if(iRetVal < 0) { UART_PRINT("bind error\n\r"); LOOP_FOREVER(); } while(FOREVER) { // // waiting on a UDP packet // iRetVal = sl_RecvFrom(iSockDesc, g_cBuffer, BUFF_SIZE, 0, ( SlSockAddr_t *)&sClientAddr, (SlSocklen_t*)&iAddrSize ); if(iRetVal > 0) { // // signal the other task about receiving the UDP packet // osi_MsgQWrite(&g_tWkupSignalQueue, &ucQueueMsg, OSI_WAIT_FOREVER); } else { UART_PRINT("recv error\n\r"); LOOP_FOREVER(); } } }
static void uart_int() { int c = UARTCharGet(CONSOLE_UART); struct prompt_event pe = {.type = PROMPT_CHAR_EVENT, .data = (void *) c}; osi_MsgQWrite(&s_v7_q, &pe, OSI_NO_WAIT); MAP_UARTIntClear(CONSOLE_UART, UART_INT_RX); } void sj_prompt_init_hal(struct v7 *v7) { (void) v7; } static void v7_task(void *arg) { struct v7 *v7 = s_v7; printf("\n\nSmart.JS for CC3200\n"); osi_MsgQCreate(&s_v7_q, "V7", sizeof(struct prompt_event), 32 /* len */); osi_InterruptRegister(CONSOLE_UART_INT, uart_int, INT_PRIORITY_LVL_1); MAP_UARTIntEnable(CONSOLE_UART, UART_INT_RX); sl_Start(NULL, NULL, NULL); v7 = s_v7 = init_v7(&v7); sj_timers_api_setup(v7); sj_v7_ext_api_setup(v7); sj_init_sys(v7); init_wifi(v7); if (init_fs(v7) != 0) { fprintf(stderr, "FS initialization failed.\n"); } mongoose_init(); sj_http_api_setup(v7); sj_i2c_api_setup(v7); /* Common config infrastructure. Mongoose & v7 must be initialized. */ init_device(v7); v7_val_t res; if (v7_exec_file(v7, "sys_init.js", &res) != V7_OK) { fprintf(stderr, "Error: "); v7_fprint(stderr, v7, res); } sj_prompt_init(v7); while (1) { struct prompt_event pe; mongoose_poll(MONGOOSE_POLL_LENGTH_MS); if (osi_MsgQRead(&s_v7_q, &pe, V7_POLL_LENGTH_MS) != OSI_OK) continue; switch (pe.type) { case PROMPT_CHAR_EVENT: { sj_prompt_process_char((char) ((int) pe.data)); break; } case V7_INVOKE_EVENT: { struct v7_invoke_event_data *ied = (struct v7_invoke_event_data *) pe.data; _sj_invoke_cb(v7, ied->func, ied->this_obj, ied->args); v7_disown(v7, &ied->args); v7_disown(v7, &ied->this_obj); v7_disown(v7, &ied->func); free(ied); break; } } } } /* Int vector table, defined in startup_gcc.c */ extern void (*const g_pfnVectors[])(void); void device_reboot(void) { sj_system_restart(0); }