/*! \brief Create TCP connection with openweathermap.org \param[in] none \return Socket descriptor for success otherwise negative \warning */ static int CreateConnection(void){ SlSockAddrIn_t Addr; INT32 sd = 0; INT32 AddrSize = 0; INT16 ret_val = 0; Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(80); /* Change the DestinationIP endianity, to big endian */ Addr.sin_addr.s_addr = sl_Htonl(appData.DestinationIP); AddrSize = sizeof(SlSockAddrIn_t); sd = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( sd < 0 ){ LCD_OutString("Error creating socket\r\n"); return sd; } ret_val = sl_Connect(sd, ( SlSockAddr_t *)&Addr, AddrSize); if( ret_val < 0 ){ /* error */ LCD_OutString("Error connecting to socket\r\n"); return ret_val; } return sd; }
int ConnectNetwork(Network* n, char* addr, int port) { SlSockAddrIn_t sAddr; int addrSize; int retVal; unsigned long ipAddress; sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET); sAddr.sin_family = AF_INET; sAddr.sin_port = sl_Htons((unsigned short)port); sAddr.sin_addr.s_addr = sl_Htonl(ipAddress); addrSize = sizeof(SlSockAddrIn_t); n->my_socket = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( n->my_socket < 0 ) { // error return -1; } retVal = sl_Connect(n->my_socket, ( SlSockAddr_t *)&sAddr, addrSize); if( retVal < 0 ) { // error sl_Close(n->my_socket); return retVal; } SysTickIntRegister(SysTickIntHandler); SysTickPeriodSet(80000); SysTickEnable(); return retVal; }
/* * ::connect() */ int connect(int socket, const struct sockaddr *address, socklen_t address_len) { SlSockAddr_t sl_address; sl_address.sa_family = address->sa_family; memcpy(sl_address.sa_data, address->sa_data, sizeof(sl_address.sa_data)); int result = sl_Connect(socket, &sl_address, address_len); if (result < 0) { switch (result) { default: { errno = EINVAL; break; } case SL_EALREADY: errno = EALREADY; break; case SL_POOL_IS_EMPTY: usleep(10000); /* fall through */ case SL_EAGAIN: errno = EAGAIN; break; } return -1; } return 0; }
int NetworkConnectTLS(Network *n, char* addr, int port, SlSockSecureFiles_t* certificates, unsigned char sec_method, unsigned int cipher, char server_verify) { SlSockAddrIn_t sAddr; int addrSize; int retVal; unsigned long ipAddress; retVal = sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET); if (retVal < 0) { return -1; } sAddr.sin_family = AF_INET; sAddr.sin_port = sl_Htons((unsigned short)port); sAddr.sin_addr.s_addr = sl_Htonl(ipAddress); addrSize = sizeof(SlSockAddrIn_t); n->my_socket = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_SEC_SOCKET); if (n->my_socket < 0) { return -1; } SlSockSecureMethod method; method.secureMethod = sec_method; retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method)); if (retVal < 0) { return retVal; } SlSockSecureMask mask; mask.secureMask = cipher; retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &mask, sizeof(mask)); if (retVal < 0) { return retVal; } if (certificates != NULL) { retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_FILES, certificates->secureFiles, sizeof(SlSockSecureFiles_t)); if (retVal < 0) { return retVal; } } retVal = sl_Connect(n->my_socket, (SlSockAddr_t *)&sAddr, addrSize); if (retVal < 0) { if (server_verify || retVal != -453) { sl_Close(n->my_socket); return retVal; } } SysTickIntRegister(SysTickIntHandler); SysTickPeriodSet(80000); SysTickEnable(); return retVal; }
//--tested, working--// //--client side--// int WiFiClient::connect(IPAddress ip, uint16_t port) { // //this function should only be called once and only on the client side // if (_socketIndex != NO_SOCKET_AVAIL) { return false; } // //get a socket index and attempt to create a socket //note that the socket is intentionally left as BLOCKING. This allows an //abusive user to send as many requests as they want as fast as they can try //and it won't overload simplelink. // int socketIndex = WiFiClass::getSocket(); if (socketIndex == NO_SOCKET_AVAIL) { return false; } int socketHandle = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_IPPROTO_TCP); if (socketHandle < 0) { return false; } // //connect the socket to the requested IP address and port. Check for success // SlSockAddrIn_t server = {0}; server.sin_family = SL_AF_INET; server.sin_port = sl_Htons(port); server.sin_addr.s_addr = ip; int iRet = sl_Connect(socketHandle, (SlSockAddr_t*)&server, sizeof(SlSockAddrIn_t)); if (iRet < 0) { sl_Close(socketHandle); return false; } int enableOption = 1; sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &enableOption, sizeof(enableOption)); sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_KEEPALIVE, &enableOption, sizeof(enableOption)); // //we've successfully created a socket and connected, so store the //information in the arrays provided by WiFiClass // _socketIndex = socketIndex; WiFiClass::_handleArray[socketIndex] = socketHandle; WiFiClass::_typeArray[socketIndex] = TYPE_TCP_CLIENT; WiFiClass::_portArray[socketIndex] = port; return true; }
int BsdTcpClient(ClientInfo_t *SpecificClientInfo) { if(CurrentClientNum >= MAX_CLIENT_NUM){ UART_PRINT("error at creating a client ! \n\r"); // error return -1; } //filling the TCP server socket address SlSockAddrIn_t sAddr; // sAddr.sin_family = SL_AF_INET; // sAddr.sin_port = sl_Htons((unsigned short)TCP_DEFAULT_PORT_NUM); // sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SERVER_ADDR); iAddrSize = sizeof(SlSockAddrIn_t); sAddr.sin_family = SL_AF_INET; sAddr.sin_port = SpecificClientInfo.port; sAddr.sin_addr.s_addr = SpecificClientInfo.brokerIP; // creating a TCP socket SpecificClientInfo.socketID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( SpecificClientInfo.socketID < 0 ) { UART_PRINT("error at creating a TCP socket ! \n\r"); // error return -1; } // connecting to TCP server if( 0 > sl_Connect(SpecificClientInfo.socketID, ( SlSockAddr_t *)&sAddr, iAddrSize)) { UART_PRINT("error at connecting to TCP server ! \n\r"); // error return -1; } return 1; }
char* HTTP_Request(const char *hostName, uint16_t port, const char *method, const char *request, char *requestData1, char *requestData2) { SlSockAddrIn_t Addr; int32_t retVal; uint32_t ASize = 0; cleanup(); strcpy(HostName, hostName); UARTprintf("\r\n\r\nUsing host: %s\r\n", HostName); retVal = sl_NetAppDnsGetHostByName(HostName, strlen(HostName),&DestinationIP, SL_AF_INET); if(retVal == 0){ Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(port); Addr.sin_addr.s_addr = sl_Htonl(DestinationIP);// IP to big endian ASize = sizeof(SlSockAddrIn_t); SockID = -1; SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( SockID >= 0 ){ retVal = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, ASize); } if((SockID >= 0)&&(retVal >= 0)){ uint32_t copyIndex = 0; strcpy(&SendBuff[copyIndex], method); copyIndex += strlen(method); strcpy(&SendBuff[copyIndex], " "); copyIndex += 1; strcpy(&SendBuff[copyIndex], request); copyIndex += strlen(request); if(requestData1) { strcpy(&SendBuff[copyIndex], requestData1); copyIndex += strlen(requestData1); } if(requestData2) { strcpy(&SendBuff[copyIndex], requestData2); copyIndex += strlen(requestData2); } strcpy(&SendBuff[copyIndex], REQ_1); copyIndex += strlen(REQ_1); strcpy(&SendBuff[copyIndex], hostName); copyIndex += strlen(hostName); strcpy(&SendBuff[copyIndex], REQ_2); copyIndex += strlen(REQ_2); SendBuff[copyIndex] = '\0'; UARTprintf("Sending request: %s\r\n\r\n", SendBuff); sl_Send(SockID, SendBuff, strlen(SendBuff), 0);// Send the HTTP GET sl_Recv(SockID, Recvbuff, MAX_RECV_BUFF_SIZE, 0);// Receive response sl_Close(SockID); return Recvbuff; } } return NULL; }
int WiFiClient::sslConnect(IPAddress ip, uint16_t port) { // //this function should only be called once and only on the client side // if (_socketIndex != NO_SOCKET_AVAIL) { return false; } // //get a socket index and attempt to create a socket //note that the socket is intentionally left as BLOCKING. This allows an //abusive user to send as many requests as they want as fast as they can try //and it won't overload simplelink. // int socketIndex = WiFiClass::getSocket(); if (socketIndex == NO_SOCKET_AVAIL) { return false; } int socketHandle = sl_Socket(SL_AF_INET, SL_SOCK_STREAM, SL_SEC_SOCKET); if (socketHandle < 0) { return false; } // Utilize rootCA file for verifying server certificate if it's been supplied with .sslRootCA() previously if (hasRootCA) { sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME, ROOTCA_PEM_FILE, strlen(ROOTCA_PEM_FILE)); } sslIsVerified = true; // //connect the socket to the requested IP address and port. Check for success // SlSockAddrIn_t server = {0}; server.sin_family = SL_AF_INET; server.sin_port = sl_Htons(port); server.sin_addr.s_addr = ip; int iRet = sl_Connect(socketHandle, (SlSockAddr_t*)&server, sizeof(SlSockAddrIn_t)); if ( iRet < 0 && (iRet != SL_ESECSNOVERIFY && iRet != SL_ESECDATEERROR) ) { sslLastError = iRet; sl_Close(socketHandle); return false; } // If the remote-end server cert could not be verified, and we demand strict verification, ABORT. if ( sslVerifyStrict && (iRet == SL_ESECSNOVERIFY || iRet == SL_ESECDATEERROR) ) { sslLastError = iRet; sl_Close(socketHandle); return false; } if (iRet == SL_ESECSNOVERIFY || iRet == SL_ESECDATEERROR) { sslLastError = iRet; sslIsVerified = false; } int enableOption = 1; sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &enableOption, sizeof(enableOption)); sl_SetSockOpt(socketHandle, SL_SOL_SOCKET, SL_SO_KEEPALIVE, &enableOption, sizeof(enableOption)); // //we've successfully created a socket and connected, so store the //information in the arrays provided by WiFiClass // _socketIndex = socketIndex; WiFiClass::_handleArray[socketIndex] = socketHandle; WiFiClass::_typeArray[socketIndex] = TYPE_TCP_CLIENT; WiFiClass::_portArray[socketIndex] = port; return true; }
//***************************************************************************** // //! This function demonstrates how certificate can be used with SSL. //! The procedure includes the following steps: //! 1) connect to an open AP //! 2) get the server name via a DNS request //! 3) define all socket options and point to the CA certificate //! 4) connect to the server via TCP //! //! \param None //! //! \return 0 on success else error code //! \return LED1 is turned solid in case of success //! LED2 is turned solid in case of failure //! //***************************************************************************** static long ssl() { SlSockAddrIn_t Addr; int iAddrSize; unsigned char ucMethod = SL_SO_SEC_METHOD_SSLV3; unsigned int uiIP,uiCipher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_SHA; long lRetVal = -1; int iSockID; GPIO_IF_LedConfigure(LED1|LED3); GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); lRetVal = InitializeAppVariables(); ASSERT_ON_ERROR(lRetVal); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // lRetVal = ConfigureSimpleLinkToDefaultState(); if(lRetVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == lRetVal) UART_PRINT("Failed to configure the device in its default state \n\r"); return lRetVal; } UART_PRINT("Device is configured in default state \n\r"); CLR_STATUS_BIT_ALL(g_ulStatus); /// // Assumption is that the device is configured in station mode already // and it is in its default state // lRetVal = sl_Start(0, 0, 0); if (lRetVal < 0 || ROLE_STA != lRetVal) { UART_PRINT("Failed to start the device \n\r"); return lRetVal; } UART_PRINT("Device started as STATION \n\r"); // //Connecting to WLAN AP // lRetVal = WlanConnect(); if(lRetVal < 0) { UART_PRINT("Failed to establish connection w/ an AP \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } UART_PRINT("Connection established w/ AP and IP is aquired \n\r"); //Set time of the device for certificate verification. lRetVal = set_time(); if(lRetVal < 0) { UART_PRINT("Unable to set time in the device"); return lRetVal; } lRetVal = sl_NetAppDnsGetHostByName(g_Host, strlen((const char *)g_Host), (unsigned long*)&uiIP, SL_AF_INET); if(lRetVal < 0) { UART_PRINT("Device couldn't retrive the host name \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(GOOGLE_DST_PORT); Addr.sin_addr.s_addr = sl_Htonl(uiIP); iAddrSize = sizeof(SlSockAddrIn_t); // // opens a secure socket // iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET); if( iSockID < 0 ) { UART_PRINT("Device unable to create secure socket \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // // configure the socket as SSLV3.0 // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &ucMethod,\ sizeof(ucMethod)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // //configure the socket as RSA with RC4 128 SHA // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &uiCipher,\ sizeof(uiCipher)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // //configure the socket with GOOGLE CA certificate - for server verification // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \ SL_SO_SECURE_FILES_CA_FILE_NAME, \ SL_SSL_CA_CERT_FILE_NAME, \ strlen(SL_SSL_CA_CERT_FILE_NAME)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \ SO_SECURE_DOMAIN_NAME_VERIFICATION, \ g_Host, strlen((const char *)g_Host)); if( lRetVal < 0 ) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } /* connect to the peer device - Google server */ lRetVal = sl_Connect(iSockID, ( SlSockAddr_t *)&Addr, iAddrSize); if(lRetVal < 0) { UART_PRINT("Device couldn't connect to Google server \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); return SUCCESS; }
int BsdTcpClient(unsigned short usPort) { UART_PRINT("BsdTcpClient\r\n"); while(serverCreatOK == 0) { //looping till simplelink starts Z_DelayMS(10); // ; UART_PRINT("serverCreatOK == 0 \n\r"); } UART_PRINT("serverCreatOK == 1\r\n"); int iCounter; short sTestBufLen; SlSockAddrIn_t sAddr; int iAddrSize; int iSockID; int iStatus; long lLoopCount = 0; long lBytesSent = 0; long lNonBlocking = 0; //0 :non-blocking, // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10); } sTestBufLen = BUF_SIZE; //filling the TCP server socket address sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)PORT_NUM); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(192,168,2,5)); iAddrSize = sizeof(SlSockAddrIn_t); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { // error return -1; } Report("creating a TCP socket yes\r\n"); iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); Report("connecting to TCP server\r\n"); // connecting to TCP server while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){ Report("connecting to TCP server error\r\n"); // vTaskSuspend(clientTaskHandle); vTaskResume((xTaskHandle)&ServerTaskHandle); } /* iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus < 0 ) { // error return -1; } */ Report("connecting to TCP server yes\r\n"); // sending 1000 packets to the TCP server while (lLoopCount < 1) { // sending packet iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 ); /* if( iStatus <= 0 ) { // error return -1; } */ vTaskResume((xTaskHandle)&ServerTaskHandle); lLoopCount++; lBytesSent += iStatus; } //closing the socket after sending 1000 packets sl_Close(iSockID); Report("closing the socket after sending 1000 packets\r\n"); //filling the TCP server socket address sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)1883); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(9,186,88,87)); iAddrSize = sizeof(SlSockAddrIn_t); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { // error return -1; } Report("creating a TCP socket yes\r\n"); iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); Report("connecting to TCP server\r\n"); // connecting to TCP server while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){ Report("connecting to TCP server error\r\n"); } /* iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus < 0 ) { // error return -1; } */ Report("connecting to TCP server yes\r\n"); //filling the TCP server socket address sAddr.sin_family = SL_AF_INET; sAddr.sin_port = sl_Htons((unsigned short)1883); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)SL_IPV4_VAL(9,186,88,87)); iAddrSize = sizeof(SlSockAddrIn_t); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { // error return -1; } Report("creating a TCP socket yes\r\n"); lNonBlocking =1; iStatus = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &lNonBlocking, sizeof(lNonBlocking)); Report("connecting to TCP server\r\n"); // connecting to TCP server while(0>sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize)){ Report("connecting to TCP server error\r\n"); } /* iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus < 0 ) { // error return -1; } */ Report("connecting to TCP server yes\r\n"); return 0; }
int main(int argc, char** argv) { SlSockAddrIn_t Addr = {0}; _u32 cipher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_SHA; _u32 googleIP = 0; _u8 method = SL_SO_SEC_METHOD_SSLV3; _i32 AddrSize = -1; _i32 g_SockID = -1; _i32 retVal = -1; retVal = initializeAppVariables(); ASSERT_ON_ERROR(retVal); /* Stop WDT and initialize the system-clock of the MCU These functions needs to be implemented in PAL */ stopWDT(); initClk(); /* Configure command line interface */ CLI_Configure(); displayBanner(); /* * Following function configures the device to default state by cleaning * the persistent settings stored in NVMEM (viz. connection profiles & * policies, power policy etc) * * Applications may choose to skip this step if the developer is sure * that the device is in its default state at start of application * * Note that all profiles and persistent settings that were done on the * device will be lost */ retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) { CLI_Write(" Failed to configure the device in its default state \n\r"); } LOOP_FOREVER(); } CLI_Write(" Device is configured in default state \n\r"); /* * Assumption is that the device is configured in station mode already * and it is in its default state */ /* Initializing the CC3100 device */ retVal = sl_Start(0, 0, 0); if ((retVal < 0) || (ROLE_STA != retVal) ) { CLI_Write(" Failed to start the device \n\r"); LOOP_FOREVER(); } CLI_Write(" Device started as STATION \n\r"); /* Connecting to WLAN AP - Set with static parameters defined at the top After this call we will be connected and have IP address */ retVal = establishConnectionWithAP(); if(retVal < 0) { CLI_Write(" Failed to establish connection w/ an AP \n\r"); LOOP_FOREVER(); } CLI_Write(" Connection established w/ AP and IP is acquired \n\r"); /* Update the CC3100 time */ retVal = SetTime(); if (retVal < 0) { CLI_Write(" Failed to set the device time \n\r"); LOOP_FOREVER(); } CLI_Write(" Establishing secure connection w/ google server \n\r"); /* get the server name via a DNS request */ retVal = sl_NetAppDnsGetHostByName(g_Google, pal_Strlen(g_Google), &googleIP, SL_AF_INET); if( retVal < 0 ) { CLI_Write(" Failed to get the IP address \n\r"); LOOP_FOREVER(); } Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(GOOGLE_DST_PORT); Addr.sin_addr.s_addr = sl_Htonl(googleIP); AddrSize = sizeof(SlSockAddrIn_t); /* opens a secure socket */ g_SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET); if( g_SockID < 0 ) { CLI_Write(" Failed to open socket \n\r"); LOOP_FOREVER(); } /* configure the socket as SSLV3.0 */ retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method)); if( retVal < 0 ) { CLI_Write(" Failed to configure the socket \n\r"); LOOP_FOREVER(); } /* configure the socket as RSA with RC4 128 SHA */ retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &cipher, sizeof(cipher)); if( retVal < 0 ) { CLI_Write(" Failed to configure the socket \n\r"); LOOP_FOREVER(); } /* configure the socket with GOOGLE CA certificate-for server verification*/ retVal = sl_SetSockOpt(g_SockID, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME, SL_SSL_CA_CERT, pal_Strlen(SL_SSL_CA_CERT)); if( retVal < 0 ) { CLI_Write(" Failed to configure the socket \n\r"); LOOP_FOREVER(); } /* connect to the peer device - GMail server */ retVal = sl_Connect(g_SockID, ( SlSockAddr_t *)&Addr, AddrSize); if (retVal < 0 ) { CLI_Write(" Failed to connect w/ google server \n\r"); LOOP_FOREVER(); } CLI_Write(" Connection w/ google server established successfully \n\r"); /* Stop the CC3100 device */ retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) LOOP_FOREVER(); return 0; }
//***************************************************************************** // //! This function demonstrates how certificate can be used with SSL. //! The procedure includes the following steps: //! 1) connect to an open AP //! 2) get the server name via a DNS request //! 3) define all socket options and point to the CA certificate //! 4) connect to the server via TCP //! //! \param None //! //! \return 0 on success else error code //! \return LED1 is turned solid in case of success //! LED2 is turned solid in case of failure //! //***************************************************************************** static int tls_connect() { SlSockAddrIn_t Addr; int iAddrSize; unsigned char ucMethod = SL_SO_SEC_METHOD_TLSV1_2; unsigned int uiIP,uiCipher = SL_SEC_MASK_TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA; long lRetVal = -1; int iSockID; lRetVal = sl_NetAppDnsGetHostByName(g_Host, strlen((const char *)g_Host), (unsigned long*)&uiIP, SL_AF_INET); if(lRetVal < 0) { UART_PRINT("Device couldn't retrive the host name \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(GOOGLE_DST_PORT); Addr.sin_addr.s_addr = sl_Htonl(uiIP); iAddrSize = sizeof(SlSockAddrIn_t); // // opens a secure socket // iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET); if( iSockID < 0 ) { UART_PRINT("Device unable to create secure socket \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // // configure the socket as TLS1.2 // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &ucMethod,\ sizeof(ucMethod)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // //configure the socket as ECDHE RSA WITH AES256 CBC SHA // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &uiCipher,\ sizeof(uiCipher)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // //configure the socket with CA certificate - for server verification // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \ SL_SO_SECURE_FILES_CA_FILE_NAME, \ SL_SSL_CA_CERT, \ strlen(SL_SSL_CA_CERT)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } //configure the socket with Client Certificate - for server verification // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \ SL_SO_SECURE_FILES_CERTIFICATE_FILE_NAME, \ SL_SSL_CLIENT, \ strlen(SL_SSL_CLIENT)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } //configure the socket with Private Key - for server verification // lRetVal = sl_SetSockOpt(iSockID, SL_SOL_SOCKET, \ SL_SO_SECURE_FILES_PRIVATE_KEY_FILE_NAME, \ SL_SSL_PRIVATE, \ strlen(SL_SSL_PRIVATE)); if(lRetVal < 0) { UART_PRINT("Device couldn't set socket options \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } /* connect to the peer device - Google server */ lRetVal = sl_Connect(iSockID, ( SlSockAddr_t *)&Addr, iAddrSize); if(lRetVal < 0) { UART_PRINT("Device couldn't connect to AWS server \n\r"); GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } else{ UART_PRINT("Device has connected to the website:"); UART_PRINT(SERVER_NAME); UART_PRINT("\n\r"); } GPIO_IF_LedOff(MCU_RED_LED_GPIO); GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); return iSockID; }
int main(void){int32_t retVal; SlSecParams_t secParams; char *pConfig = NULL; INT32 ASize = 0; SlSockAddrIn_t Addr; ADC0_InitSWTriggerSeq3_Ch9(); // allow time to finish activating initClk(); // PLL 50 MHz Output_On(); UART_Init(); // Send data to PC, 115200 bps Timer1_Init(); LED_Init(); // initialize LaunchPad I/O UARTprintf("Weather App\n"); retVal = configureSimpleLinkToDefaultState(pConfig); // set policies if(retVal < 0)Crash(4000000); retVal = sl_Start(0, pConfig, 0); if((retVal < 0) || (ROLE_STA != retVal) ) Crash(8000000); secParams.Key = PASSKEY; secParams.KeyLen = strlen(PASSKEY); secParams.Type = SEC_TYPE; // OPEN, WPA, or WEP sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0); while((0 == (g_Status&CONNECTED)) || (0 == (g_Status&IP_AQUIRED))){ _SlNonOsMainLoopTask(); } UARTprintf("Connected\n"); while(1){ int i = 0; while(i < 10){ int sendc = 0; strcpy(HostName,"openweathermap.org"); retVal = sl_NetAppDnsGetHostByName(HostName, strlen(HostName),&DestinationIP, SL_AF_INET); if(retVal == 0){ Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(80); Addr.sin_addr.s_addr = sl_Htonl(DestinationIP);// IP to big endian ASize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( SockID >= 0 ){ retVal = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, ASize); } if((SockID >= 0)&&(retVal >= 0)){ strcpy(SendBuff,REQUEST); sl_Send(SockID, SendBuff, strlen(SendBuff), 0);// Send the HTTP GET sl_Recv(SockID, Recvbuff, MAX_RECV_BUFF_SIZE, 0);// Receive response sl_Close(SockID); LED_GreenOn(); UARTprintf("\r\n\r\n"); UARTprintf(Recvbuff); UARTprintf("\r\n"); } } ST7735_OutUDec(sendc); ST7735_OutString("\n"); i++; } //while(Board_Input()==0){}; // wait for touch LED_GreenOff(); //Temp Part e getTemp(Recvbuff); ST7735_OutChar('T'); ST7735_OutChar('e'); ST7735_OutChar('m'); ST7735_OutChar('p'); ST7735_OutChar(' '); ST7735_OutChar('='); ST7735_OutChar(' '); for(int i = 0; i < 5; i++){ ST7735_OutChar(myArray[i]); } ST7735_OutChar('\n'); //ADC Part f ADC0_SAC_R = ADC_SAC_AVG_64X; //enable 64 times average before obtaining result int voltage = ADC0_InSeq3(); ST7735_OutString("Voltage~"); ST7735_sDecOut3(voltage); char* voltageString; char voltageStringNum[5]; sprintf(voltageStringNum, "%.1d.%.3d", voltage/1000, voltage%1000); //ST7735_OutString(voltageStringNum); char* sendString; char str1[173] = "GET /query?city=Austin%20Texas&id=Ty%20Winkler%20Jeremiah%20Bartlett&greet=Voltage%3D"; strcat(str1, voltageStringNum); strcat(str1, "V&edxcode=8086 HTTP/1.1\r\nUser-Agent: Keil\r\nHost: embsysmooc.appspot.com\r\n\r\n"); strcpy(HostName,"embsysmooc.appspot.com"); retVal = sl_NetAppDnsGetHostByName(HostName, strlen(HostName),&DestinationIP, SL_AF_INET); if(retVal == 0){ Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(80); Addr.sin_addr.s_addr = sl_Htonl(DestinationIP);// IP to big endian ASize = sizeof(SlSockAddrIn_t); SockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( SockID >= 0 ){ retVal = sl_Connect(SockID, ( SlSockAddr_t *)&Addr, ASize); } if((SockID >= 0)&&(retVal >= 0)){ strcpy(SendBuff, str1); count = 0; sl_Send(SockID, SendBuff, strlen(SendBuff), 0);// Send the HTTP GET sl_Recv(SockID, Recvbuff, MAX_RECV_BUFF_SIZE, 0);// Receive response sl_Close(SockID); LED_GreenOn(); UARTprintf("\r\n\r\n"); //ST7735_OutString(Recvbuff); UARTprintf("\r\n"); } } while(1); } }
//***************************************************************************** // //! This function POST to Event Hub REST API using TLS //! //! \param None //! //! \return 0 on success else error code //! \return Error number on Failure // //***************************************************************************** long PostEventHubSSL() { SlSockAddrIn_t Addr; int iAddrSize; unsigned char ucMethod = SL_SO_SEC_METHOD_TLSV1; //SL_SO_SEC_METHOD_SSLv3_TLSV1_2; //SL_SO_SEC_METHOD_TLSV1_2; //SL_SO_SEC_METHOD_SSLV3; unsigned int uiIP; unsigned int uiCipher = SL_SEC_MASK_SSL_RSA_WITH_RC4_128_SHA; long lRetVal = -1; int iSSLSockID; char acSendBuff[512]; char acRecvbuff[1460]; //char* pcBufData; char* pcBufHeaders; // // Retrieve IP from Hostname // lRetVal = sl_NetAppDnsGetHostByName(g_Host, strlen((const char *)g_Host), (unsigned long*)&uiIP, SL_AF_INET); if(lRetVal < 0) { CLI_Write("Could not retrive the IP Address for Azure Server.\n\r"); //GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } Addr.sin_family = SL_AF_INET; Addr.sin_port = sl_Htons(SSL_DST_PORT); Addr.sin_addr.s_addr = sl_Htonl(uiIP); iAddrSize = sizeof(SlSockAddrIn_t); // // Opens a secure socket // iSSLSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET); if( iSSLSockID < 0 ) { CLI_Write("Unable to create secure socket.\n\r"); //GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // // Configure the socket as TLS (SSLV3.0 does not work because of POODLE - http://en.wikipedia.org/wiki/POODLE) // lRetVal = sl_SetSockOpt(iSSLSockID, SL_SOL_SOCKET, SL_SO_SECMETHOD, &ucMethod, sizeof(ucMethod)); if(lRetVal < 0) { CLI_Write("Couldn't set socket option (TLS).\n\r"); sl_Close(iSSLSockID); //GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // // Configure the socket as RSA with RC4 128 SHA // lRetVal = sl_SetSockOpt(iSSLSockID, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &uiCipher, sizeof(uiCipher)); if(lRetVal < 0) { CLI_Write("Couldn't set socket option (RSA).\n\r"); sl_Close(iSSLSockID); return lRetVal; } // // Configure the socket with Azure CA certificate - for server verification // lRetVal = sl_SetSockOpt(iSSLSockID, SL_SOL_SOCKET, SL_SO_SECURE_FILES_CA_FILE_NAME, SL_SSL_CA_CERT_FILE_NAME, strlen(SL_SSL_CA_CERT_FILE_NAME)); if(lRetVal < 0) { CLI_Write("Couldn't set socket option (CA Certificate).\n\r"); sl_Close(iSSLSockID); return lRetVal; } // // Configure the recieve timeout // struct SlTimeval_t timeVal; timeVal.tv_sec = SERVER_RESPONSE_TIMEOUT; // In Seconds timeVal.tv_usec = 0; // Microseconds. 10000 microseconds resolution lRetVal = sl_SetSockOpt(iSSLSockID, SL_SOL_SOCKET, SL_SO_RCVTIMEO, (unsigned char*)&timeVal, sizeof(timeVal)); if(lRetVal < 0) { CLI_Write("Couldn't set socket option (Receive Timeout).\n\r"); sl_Close(iSSLSockID); return lRetVal; } // // Connect to the peer device - Azure server */ // lRetVal = sl_Connect(iSSLSockID, ( SlSockAddr_t *)&Addr, iAddrSize); if(lRetVal < 0) { CLI_Write("Couldn't connect to Azure server.\n\r"); sl_Close(iSSLSockID); //GPIO_IF_LedOn(MCU_RED_LED_GPIO); return lRetVal; } // // Generate a random number for the temperture // srand((unsigned int)time(NULL)); float a = 5.0; float fRandTemp = 25 - (((float)rand()/(float)(RAND_MAX)) * a); char cTempChar[5]; sprintf(cTempChar, "%.2f", fRandTemp); // // Creates the HTTP POST string. // int dataLength = strlen(DATA1) + 5 + strlen(DATA2); char cCLLength[4]; pcBufHeaders = acSendBuff; strcpy(pcBufHeaders, POSTHEADER); pcBufHeaders += strlen(POSTHEADER); strcpy(pcBufHeaders, HOSTHEADER); pcBufHeaders += strlen(HOSTHEADER); strcpy(pcBufHeaders, AUTHHEADER); pcBufHeaders += strlen(AUTHHEADER); strcpy(pcBufHeaders, CHEADER); pcBufHeaders += strlen(CHEADER); strcpy(pcBufHeaders, CTHEADER); pcBufHeaders += strlen(CTHEADER); strcpy(pcBufHeaders, CLHEADER1); pcBufHeaders += strlen(CLHEADER1); sprintf(cCLLength, "%d", dataLength); strcpy(pcBufHeaders, cCLLength); pcBufHeaders += strlen(cCLLength); strcpy(pcBufHeaders, CLHEADER2); pcBufHeaders += strlen(CLHEADER2); strcpy(pcBufHeaders, DATA1); pcBufHeaders += strlen(DATA1); strcpy(pcBufHeaders , cTempChar); pcBufHeaders += strlen(cTempChar); strcpy(pcBufHeaders, DATA2); int testDataLength = strlen(pcBufHeaders); // // Send the packet to the server */ // lRetVal = sl_Send(iSSLSockID, acSendBuff, strlen(acSendBuff), 0); if(lRetVal < 0) { CLI_Write("POST failed.\n\r"); sl_Close(iSSLSockID); return lRetVal; } // // Receive response packet from the server */ // lRetVal = sl_Recv(iSSLSockID, &acRecvbuff[0], sizeof(acRecvbuff), 0); if(lRetVal < 0) { CLI_Write("Received failed.\n\r"); sl_Close(iSSLSockID); return lRetVal; } else { CLI_Write("HTTP POST Successful. Telemetry successfully logged\n\r"); } sl_Close(iSSLSockID); return SUCCESS; }
/**************************************************************************** // //! \brief Opening a TCP client side socket and sending data //! //! This function opens a TCP socket and tries to connect to a Server IP_ADDR //! waiting on port PORT_NUM. //! If the socket connection is successful then the function will send 1000 //! TCP packets to the server. //! //! \param[in] port number on which the server will be listening on //! //! \return 0 on success, -1 on Error. // ****************************************************************************/ int BsdTcpClient(unsigned short usPort) { int iCounter; short sTestBufLen; SlSockAddrIn_t sAddr; int iAddrSize; int iSockID; int iStatus; unsigned long lLoopCount = 0; long lBytesSent = 0; // filling the buffer for (iCounter=0 ; iCounter<BUF_SIZE ; iCounter++) { g_cBsdBuf[iCounter] = (char)(iCounter % 10); } sTestBufLen = BUF_SIZE; //filling the TCP server socket address sAddr.sin_family = SL_AF_INET; // sAddr.sin_port = usPort; // sAddr.sin_addr.s_addr = IP_ADDR ; sAddr.sin_port = sl_Htons((unsigned short)usPort); sAddr.sin_addr.s_addr = sl_Htonl((unsigned int)IP_ADDR); iAddrSize = sizeof(SlSockAddrIn_t); // creating a TCP socket iSockID = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0); if( iSockID < 0 ) { UART_PRINT("error at creating a TCP socket ! \n\r"); // error return -1; } UART_PRINT("iSockID :"); Z_NumDispaly(iSockID, 2); // connecting to TCP server iStatus = sl_Connect(iSockID, ( SlSockAddr_t *)&sAddr, iAddrSize); if( iStatus < 0 ) { UART_PRINT("error at connecting to TCP server ! \n\r"); // error return -1; } UART_PRINT(" connected to TCP server ok! \n\r"); // sending 1000 packets to the TCP server while (lLoopCount < 1000) { // sending packet iStatus = sl_Send(iSockID, g_cBsdBuf, sTestBufLen, 0 ); if( iStatus <= 0 ) { UART_PRINT("error at sending packet"); Z_NumDispaly(lLoopCount,2); // error return -1; } lLoopCount++; lBytesSent += iStatus; } //closing the socket after sending 1000 packets sl_Close(iSockID); return 0; }