Port from Avnet's Internet Of Things full WiGo demo: SmartConfig - WebServer - Exosite - Android sensor Fusion App
Dependencies: mbed CC3000_Hostdriver TEMT6200 TSI Wi-Go_eCompass_Lib_V3 WiGo_BattCharger
Fork of CC3000_Simple_Socket by
Information
This demo uses the old HostDriver.
A newer release using the mbed socket compatible API HostDriver is available at Wi-Go_IOT_Demo_MKII.
Wi-Go Reference Design Overview
For additional information on Wi-Go, please visit http://www.em.avnet.com/wi-go
For additional information on Freescale eCompass, please visit
http://www.freescale.com/webapp/sps/site/prod_summary.jsp?code=E-Compass
Ported from Avnet's Wi-Go KEIL code.
Special thanks to Jim Carver from Avnet for providing the Wi-Go board and for his assistance.
Multiple Wi-Fi applications are provided within the latest version of Wi-Go software:
- SmartConfig App for auto-setup of Wi-Go network parameters.
- WebServer display of live sensor data.
- Exosite portal sensor data feed by Wi-Go.
- Freescale's Sensor Fusion App data feed by Wi-Go.
Wi-Go is intended for "untethered" portable operation (using it's high-capacity Lithium-Polymer battery). The serial terminal text interface is only required for initial setup, thereafter selection of an application from those available is via finger position on the Touch Slider during the initial 6 second startup period.
Running the Wi-Go Demo Suite
Warning
- We need a large amount of free RAM for the eCompass library:
Before compiling the code, check ifCC3000_MAXIMAL_RX_SIZE
is set to(511 + 1)
incc3000_common.h
.
- The on-board Firmware must be updated to mbed enable a Wi-Go system. Goto the Component page to get the FirmwareUpdate tool (scroll down to the FirmwareUpdate topic).
MAG3110 sensor and eCompass Calibration!
As with the other sensor applications, the eCompass function requires quality calibration data to achieve best accuracy.
For the first 15 seconds after power-up it is recommended that "Figure 8" movements with Wi-Go be done in a smooth, repetitive pattern. Don't touch the slider pad during calibration.
Startup
The RGB LED blinks in a GREEN-ORANGE sequence to inform the user the module is waiting for input.
The RGB LED color designates which of the following Apps to launch.
RGB LED Color | Application to Launch |
Purple | SmartConfig |
Blue | WebServer |
Red | Exosite Data Client |
Green | Android Server |
Swipe your index finger across the slider pad, the RGB LED color will change at approximately 25% intervals.
Removing your finger latches the last color displayed. After about 3 seconds, the selected app will start.
Another app can be selected when the slider pad is touched again within the 3 seconds timeout.
After launch of Exosite or Android Server Apps, the eCompass function then controls the RGB LED.
(not in WebServer mode where RGB LEDs are manually controlled by the User).
RGB LED Color | Direction Indication |
Blue | Near to North |
Green | North |
Red | East / West |
Purple | South |
__Note!__
The D1, D2 and D3 User LEDs on Wi-Go adhere to the following convention for the different Apps
User LED# | Description of function controlling the LED |
D1 | is the board heartbeat, derived from the timer interrupt |
D2 | indicates network activity as follows: Web Server Wi-Go webpage is being served. Exosite Client Wi-Go is sending data. Android App Wi-Go is sending data |
D3 | WLAN Network is Connected |
Detail of Wi-Go Applications
App #1: SmartConfig
See TI's pages on how to use the SmartConfig tool:
- Preferred method : Configuration using the SmartConfig tool
- SmartConfig download: Smart Config and Home Automation
- iOS app : available at Apple app store.
- Android app : download and install the Android SmartConfig Application on a PC.
This file contains the source code as well as the compiled APK file.
The APK file is stored inti\CC3000AndroidApp\SmartConfigCC3X\bin
.
- iOS app : available at Apple app store.
App #2: WebServer display of live sensor data
__Note!__
When using the WebServer for the first time on a Wi-Fi network you will need to determine the IP address that's assigned to Wi-Go by the DHCP Server. To do this, it is recommended you use one of the following two methods:
- While Wi-Go is initially tethered to a laptop via USB, launch of the WebServer Application and note the IP address that is reported on the terminal screen immediately after selection of this App.
- Alternatively, use a 3rd party LAN SCAN type tool to view Wi-Go's IP address.
eg.FING
, - available for free download from Google Play or iTunes App Stores…
Wi-Go's WebServer Application is selected as follows:
- Press RESET, followed by the
eCompass Calibration
(mentioned at the top of this page).
Then use index finger on slider to select the WebServer App (RGB LED = BLUE).
At end of the 3 second selection period the WebServer App shall launch.
- If you are tethered to a laptop and have a terminal open the Wi-Fi network connection confirmation will be seen, eg.
'*** Wi-Go board DHCP assigned IP Address = 192.168.43.102
- Once you have noted Wi-Go's reported IP address, the USB cable may be disconnected and Wi-Go then used as intended, running on it's own battery power.
- Use an Internet Browser on SmartPhone/Tablet/Laptop (connected to same Hot-Spot/Wireless Router subnet), to now connect to the noted Wi-Go IP address and view the WebServer output:
- the Webserver sensor data is auto-updated every 2 seconds a manual refresh (F5 on laptop).
- In the event of an error, press refresh to regenerate the screen.
- Use the mouse (or touch-screen) to exercise the RGB LED output.
App #3: Exosite Data Client
Wi-Go's sensor data gets transmitted via Wi-Fi to a cloud-based Exosite portal where the sensor measurements are displayed graphically on a "dashboard". Users can create unique customized dashboards using drag and drop GUI widgets from the library provided on the Exosite website.
__Note!__
For the Exosite application a "live" connection to the Internet is required !!!
- Press RESET, followed by the
eCompass Calibration
(mentioned at the top of this page).
Then use index finger on slider to select the Exosite Client App (RGB LED = RED) - On launching this App, note Wi-Go's MAC address displayed on your terminal
(if not running a terminal use FING or other WLAN Scan tool to determine Wi-Go's MAC address) - Using your computer's internet browser, go to avnet.exosite.com and sign-up for a free
Avnet Trial
Exosite Account: - On the next screen, click on the Sign-Up Now button in the displayed
Avnet Trial
account option. - Complete the Account Info and Contact Info then click on Create Account (make sure to use a valid email address!).
- Check for new incoming email from avnet.exosite.com to the address you provided and click on the link in this email to activate your new Exosite account.
- Once activated, login using the email address and password that you chose in your registration.
Your Exosite Portal and Dashboard should now display. The first time you log-in to your new account, the default Home dashboard will be displayed, pre-configured with two widgets. On the left is the Welcome widget for tips and information. On the right is the Device List widget.
Dashboards are configurable, so at any time this default dashboard can be changed, widgets deleted and added (Clicking the upside-down triangle icon in a widget's Title bar will allow you to edit it). - Before going further with the Dashboard, you need to connect your Wi-Go device to your Exosite account. Do this by going to the left sidebar and selecting Devices followed by selecting the +Add Device link (on right of screen).
- In the Setup screens that follow, enter the following
Select a supported device | Wi-Go |
Enter device MAC Address | nn:nn:nn:nn:nn:nn [your Wi-Go's MAC address including colons] |
Enter device Name | [choose a descriptive name] |
Enter device Location | [description of your location] |
- Once completed, under Devices the name chosen for the added Wi-Go device should now be listed.
- Click on this new Wi-Go device to examine (and edit if necessary) it's Device Information screen.
- Click the CLOSE button to exit the Device Information screen.
- On your Wi-Go kit now press RESET, followed by the
eCompass Calibration
(mentioned at the top of this page)
and again select the Exosite Client App (RGB LED = RED) using your index finger. - Refresh your browser (press F5) a couple've times until the Active indicator changes to On (Green).
- From the left sidebar click on Home and click on the recently named Wi-Go device which is located under the Device List.
This will bring-up a default dashboard display similar to what's shown below.
(Dashboards are typically accessed via the Dashboards menu entry). Check the dashboard is updating with live data by moving your Wi-Go Kit through different orientations.
- To create a custom dashboard, select Dashboards from the sidebar menu, followed by +Add Dashboard (on right side of Your Dashboards title bar). After completion of the initial configuration screen you will then be able to add Widgets to display the various Wi-Go data sources as well as pictures and text to support your application.
- More guidance on the creation, editing and sharing of custom dashboards is available under the Exosite support pages
App #4: Android Sensor Fusion App
- Press RESET, followed by the
eCompass Calibration
(mentioned at the top of this page)
, then use index finger on slider to select the Android App (RGB LED = GREEN)
- Freescale's ''Xtrinsic Sensor Fusion Toolbox'" will run on Android 3.0 or above phone or tablet. Free to download from Google Play, type Sensor fusion in the search box to find it. freescale.sensors.sfusion
- The Freescale App is well documented. To access the built-in documentation, press the NAV button at top of screen followed by Documentation from the scroll-down menu:
- Freescale's sensors site provides additional resources such as this overview:
free-android-app-teaches-sensor-fusion-basics
- Go to the Options Menu and select Preferences…
- The following items need to be taken care of:
Enter WiGo's IP address |
Enter the SSID (of the Hot-Spot or Wireless Access Point used by Wi-Go) |
- Press Save and Exit!
- Exit the Application completely then re-launch the Sensor Fusion Application.
- Select the ''Source/Algorithm'" menu and change the data source to Wi-Go mag/accel
- The Android App should now be displaying a 3-D image of Wi-Go that you can rotate and flip-over by moving the Wi-Go board accordingly…
- Use NAV > Device View to display if this view does not come-up by default.
- A Serial Terminal connection is not necessary but if you happen to have one open you should see the following messages as Wi-Go connects to the Android App:
"Server waiting for connection" followed by
"connected, transmit buffer size= 96", and then
"input = 0123456789"
at which time Wi-Go starts streaming data to the Android App.
Webserver/exosite.cpp@3:405462258899, 2013-09-07 (annotated)
- Committer:
- frankvnk
- Date:
- Sat Sep 07 15:41:49 2013 +0000
- Revision:
- 3:405462258899
- Child:
- 6:7c06ad22f206
First release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frankvnk | 3:405462258899 | 1 | /***************************************************************************** |
frankvnk | 3:405462258899 | 2 | * |
frankvnk | 3:405462258899 | 3 | * exosite.c - Exosite cloud communications. |
frankvnk | 3:405462258899 | 4 | * Copyright (C) 2012 Exosite LLC |
frankvnk | 3:405462258899 | 5 | * |
frankvnk | 3:405462258899 | 6 | * Redistribution and use in source and binary forms, with or without |
frankvnk | 3:405462258899 | 7 | * modification, are permitted provided that the following conditions |
frankvnk | 3:405462258899 | 8 | * are met: |
frankvnk | 3:405462258899 | 9 | * |
frankvnk | 3:405462258899 | 10 | * Redistributions of source code must retain the above copyright |
frankvnk | 3:405462258899 | 11 | * notice, this list of conditions and the following disclaimer. |
frankvnk | 3:405462258899 | 12 | * |
frankvnk | 3:405462258899 | 13 | * Redistributions in binary form must reproduce the above copyright |
frankvnk | 3:405462258899 | 14 | * notice, this list of conditions and the following disclaimer in the |
frankvnk | 3:405462258899 | 15 | * documentation and/or other materials provided with the |
frankvnk | 3:405462258899 | 16 | * distribution. |
frankvnk | 3:405462258899 | 17 | * |
frankvnk | 3:405462258899 | 18 | * Neither the name of Texas Instruments Incorporated nor the names of |
frankvnk | 3:405462258899 | 19 | * its contributors may be used to endorse or promote products derived |
frankvnk | 3:405462258899 | 20 | * from this software without specific prior written permission. |
frankvnk | 3:405462258899 | 21 | * |
frankvnk | 3:405462258899 | 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
frankvnk | 3:405462258899 | 23 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
frankvnk | 3:405462258899 | 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
frankvnk | 3:405462258899 | 25 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
frankvnk | 3:405462258899 | 26 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
frankvnk | 3:405462258899 | 27 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
frankvnk | 3:405462258899 | 28 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
frankvnk | 3:405462258899 | 29 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
frankvnk | 3:405462258899 | 30 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
frankvnk | 3:405462258899 | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
frankvnk | 3:405462258899 | 32 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
frankvnk | 3:405462258899 | 33 | * |
frankvnk | 3:405462258899 | 34 | *****************************************************************************/ |
frankvnk | 3:405462258899 | 35 | |
frankvnk | 3:405462258899 | 36 | #include "doTCPIP.h" |
frankvnk | 3:405462258899 | 37 | #include "exosite.h" |
frankvnk | 3:405462258899 | 38 | #include "exosite_hal.h" |
frankvnk | 3:405462258899 | 39 | #include "exosite_meta.h" |
frankvnk | 3:405462258899 | 40 | #include "strlib.h" |
frankvnk | 3:405462258899 | 41 | |
frankvnk | 3:405462258899 | 42 | extern userFS_t userFS; |
frankvnk | 3:405462258899 | 43 | |
frankvnk | 3:405462258899 | 44 | //local defines |
frankvnk | 3:405462258899 | 45 | #define RX_SIZE 60 |
frankvnk | 3:405462258899 | 46 | |
frankvnk | 3:405462258899 | 47 | #define MAC_LEN 6 |
frankvnk | 3:405462258899 | 48 | |
frankvnk | 3:405462258899 | 49 | typedef enum |
frankvnk | 3:405462258899 | 50 | { |
frankvnk | 3:405462258899 | 51 | CIK_LINE, |
frankvnk | 3:405462258899 | 52 | HOST_LINE, |
frankvnk | 3:405462258899 | 53 | CONTENT_LINE, |
frankvnk | 3:405462258899 | 54 | ACCEPT_LINE, |
frankvnk | 3:405462258899 | 55 | LENGTH_LINE, |
frankvnk | 3:405462258899 | 56 | GETDATA_LINE, |
frankvnk | 3:405462258899 | 57 | POSTDATA_LINE, |
frankvnk | 3:405462258899 | 58 | VENDOR_LINE, |
frankvnk | 3:405462258899 | 59 | EMPTY_LINE |
frankvnk | 3:405462258899 | 60 | } lineTypes; |
frankvnk | 3:405462258899 | 61 | |
frankvnk | 3:405462258899 | 62 | #define STR_CIK_HEADER "X-Exosite-CIK: c3c675e0601551c6d4d3230e162d62cc8bba3311\r\n" |
frankvnk | 3:405462258899 | 63 | #define STR_CONTENT_LENGTH "Content-Length: " |
frankvnk | 3:405462258899 | 64 | #define STR_GET_URL "GET /api:v1/stack/alias?" |
frankvnk | 3:405462258899 | 65 | #define STR_HTTP " HTTP/1.1\r\n" |
frankvnk | 3:405462258899 | 66 | #define STR_HOST "Host: avnet.m2.exosite.com\r\n" |
frankvnk | 3:405462258899 | 67 | #define STR_ACCEPT "Accept: application/x-www-form-urlencoded; charset=utf-8\r\n" |
frankvnk | 3:405462258899 | 68 | #define STR_CONTENT "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n" |
frankvnk | 3:405462258899 | 69 | #define STR_VENDOR "vendor=avnet&model=wigosmartconfig&sn=" |
frankvnk | 3:405462258899 | 70 | #define STR_CRLF "\r\n" |
frankvnk | 3:405462258899 | 71 | #define MY_CIK "27b9684751332589be52c8e5819e7c438f7e7479" |
frankvnk | 3:405462258899 | 72 | char myCIK[] = "c3c675e0601551c6d4d3230e162d62cc8bba3311"; |
frankvnk | 3:405462258899 | 73 | |
frankvnk | 3:405462258899 | 74 | |
frankvnk | 3:405462258899 | 75 | // local functions |
frankvnk | 3:405462258899 | 76 | void activate_device(void); |
frankvnk | 3:405462258899 | 77 | void init_mac_address(unsigned char if_nbr); |
frankvnk | 3:405462258899 | 78 | void update_m2ip(void); |
frankvnk | 3:405462258899 | 79 | int readResponse(long socket, char * expectedCode); |
frankvnk | 3:405462258899 | 80 | long connect_to_exosite(void); |
frankvnk | 3:405462258899 | 81 | void sendLine(long socket, unsigned char LINE, char * payload); |
frankvnk | 3:405462258899 | 82 | |
frankvnk | 3:405462258899 | 83 | // global functions |
frankvnk | 3:405462258899 | 84 | int Exosite_Write(char * pbuf, unsigned char bufsize); |
frankvnk | 3:405462258899 | 85 | int Exosite_Read(char * palias, char * pbuf, unsigned char bufsize); |
frankvnk | 3:405462258899 | 86 | int Exosite_Init(void); |
frankvnk | 3:405462258899 | 87 | int Exosite_ReInit(void); |
frankvnk | 3:405462258899 | 88 | void Exosite_SetCIK(char * pCIK); |
frankvnk | 3:405462258899 | 89 | |
frankvnk | 3:405462258899 | 90 | // externs |
frankvnk | 3:405462258899 | 91 | extern char *itoa(int n, char *s, int b); |
frankvnk | 3:405462258899 | 92 | extern char activeCIK[]; |
frankvnk | 3:405462258899 | 93 | extern int server_running; |
frankvnk | 3:405462258899 | 94 | |
frankvnk | 3:405462258899 | 95 | // global variables |
frankvnk | 3:405462258899 | 96 | static unsigned char exositeWriteFailures = 0; |
frankvnk | 3:405462258899 | 97 | |
frankvnk | 3:405462258899 | 98 | |
frankvnk | 3:405462258899 | 99 | //***************************************************************************** |
frankvnk | 3:405462258899 | 100 | // |
frankvnk | 3:405462258899 | 101 | //! Exosite_Init |
frankvnk | 3:405462258899 | 102 | //! |
frankvnk | 3:405462258899 | 103 | //! \param None |
frankvnk | 3:405462258899 | 104 | //! |
frankvnk | 3:405462258899 | 105 | //! \return 0 success; -1 failure |
frankvnk | 3:405462258899 | 106 | //! |
frankvnk | 3:405462258899 | 107 | //! \brief The function initializes the cloud connection to Exosite |
frankvnk | 3:405462258899 | 108 | // |
frankvnk | 3:405462258899 | 109 | //***************************************************************************** |
frankvnk | 3:405462258899 | 110 | int Exosite_Init(void) |
frankvnk | 3:405462258899 | 111 | { |
frankvnk | 3:405462258899 | 112 | exosite_meta_init(); //always initialize our meta structure |
frankvnk | 3:405462258899 | 113 | init_mac_address(IF_WIFI); //always check to see if the MAC is up to date |
frankvnk | 3:405462258899 | 114 | |
frankvnk | 3:405462258899 | 115 | //setup some of our globals for operation |
frankvnk | 3:405462258899 | 116 | exositeWriteFailures = 0; |
frankvnk | 3:405462258899 | 117 | |
frankvnk | 3:405462258899 | 118 | return Exosite_ReInit(); |
frankvnk | 3:405462258899 | 119 | } |
frankvnk | 3:405462258899 | 120 | |
frankvnk | 3:405462258899 | 121 | |
frankvnk | 3:405462258899 | 122 | //***************************************************************************** |
frankvnk | 3:405462258899 | 123 | // |
frankvnk | 3:405462258899 | 124 | //! Exosite_ReInit |
frankvnk | 3:405462258899 | 125 | //! |
frankvnk | 3:405462258899 | 126 | //! \param None |
frankvnk | 3:405462258899 | 127 | //! |
frankvnk | 3:405462258899 | 128 | //! \return 0 success; -1 failure |
frankvnk | 3:405462258899 | 129 | //! |
frankvnk | 3:405462258899 | 130 | //! \brief Called after Init has been ran in the past, but maybe comms were |
frankvnk | 3:405462258899 | 131 | //! down and we have to keep trying... |
frankvnk | 3:405462258899 | 132 | // |
frankvnk | 3:405462258899 | 133 | //***************************************************************************** |
frankvnk | 3:405462258899 | 134 | int Exosite_ReInit(void) |
frankvnk | 3:405462258899 | 135 | { |
frankvnk | 3:405462258899 | 136 | int i; |
frankvnk | 3:405462258899 | 137 | char tempCIK[CIK_LENGTH]; |
frankvnk | 3:405462258899 | 138 | update_m2ip(); //check our IP api to see if the old IP is advertising a new one |
frankvnk | 3:405462258899 | 139 | activate_device(); //the moment of truth - can this device provision with the Exosite cloud?... |
frankvnk | 3:405462258899 | 140 | |
frankvnk | 3:405462258899 | 141 | //sanity check on the CIK |
frankvnk | 3:405462258899 | 142 | exosite_meta_read((unsigned char *)tempCIK, CIK_LENGTH, META_CIK); |
frankvnk | 3:405462258899 | 143 | for (i = 0; i < CIK_LENGTH; i++) |
frankvnk | 3:405462258899 | 144 | { |
frankvnk | 3:405462258899 | 145 | if (!(tempCIK[i] >= 'a' && tempCIK[i] <= 'f' || tempCIK[i] >= '0' && tempCIK[i] <= '9')) |
frankvnk | 3:405462258899 | 146 | { |
frankvnk | 3:405462258899 | 147 | return -1; |
frankvnk | 3:405462258899 | 148 | } |
frankvnk | 3:405462258899 | 149 | } |
frankvnk | 3:405462258899 | 150 | for(i=0;i<CIK_LENGTH;i++) userFS.CIK[i] = tempCIK[i]; |
frankvnk | 3:405462258899 | 151 | userFS.validCIK = 1; |
frankvnk | 3:405462258899 | 152 | nvmem_write( NVMEM_USER_FILE_1_FILEID, sizeof(userFS), 0, (unsigned char *) &userFS); |
frankvnk | 3:405462258899 | 153 | return 0; |
frankvnk | 3:405462258899 | 154 | } |
frankvnk | 3:405462258899 | 155 | |
frankvnk | 3:405462258899 | 156 | |
frankvnk | 3:405462258899 | 157 | //***************************************************************************** |
frankvnk | 3:405462258899 | 158 | // |
frankvnk | 3:405462258899 | 159 | //! Exosite_SetCIK |
frankvnk | 3:405462258899 | 160 | //! |
frankvnk | 3:405462258899 | 161 | //! \param pointer to CIK |
frankvnk | 3:405462258899 | 162 | //! |
frankvnk | 3:405462258899 | 163 | //! \return None |
frankvnk | 3:405462258899 | 164 | //! |
frankvnk | 3:405462258899 | 165 | //! \brief Programs a new CIK to flash / non volatile |
frankvnk | 3:405462258899 | 166 | // |
frankvnk | 3:405462258899 | 167 | //***************************************************************************** |
frankvnk | 3:405462258899 | 168 | void |
frankvnk | 3:405462258899 | 169 | Exosite_SetCIK(char * pCIK) |
frankvnk | 3:405462258899 | 170 | { |
frankvnk | 3:405462258899 | 171 | exosite_meta_write((unsigned char *)pCIK, CIK_LENGTH, META_CIK); |
frankvnk | 3:405462258899 | 172 | } |
frankvnk | 3:405462258899 | 173 | |
frankvnk | 3:405462258899 | 174 | |
frankvnk | 3:405462258899 | 175 | //***************************************************************************** |
frankvnk | 3:405462258899 | 176 | // |
frankvnk | 3:405462258899 | 177 | //! Exosite_Write |
frankvnk | 3:405462258899 | 178 | //! |
frankvnk | 3:405462258899 | 179 | //! \param pbuf - string buffer containing data to be sent |
frankvnk | 3:405462258899 | 180 | //! bufsize - number of bytes to send |
frankvnk | 3:405462258899 | 181 | //! |
frankvnk | 3:405462258899 | 182 | //! \return 0 success; -1 failure |
frankvnk | 3:405462258899 | 183 | //! |
frankvnk | 3:405462258899 | 184 | //! \brief The function writes data to Exosite |
frankvnk | 3:405462258899 | 185 | // |
frankvnk | 3:405462258899 | 186 | //***************************************************************************** |
frankvnk | 3:405462258899 | 187 | int |
frankvnk | 3:405462258899 | 188 | Exosite_Write(char * pbuf, unsigned char bufsize) |
frankvnk | 3:405462258899 | 189 | { |
frankvnk | 3:405462258899 | 190 | char strBuf[10]; |
frankvnk | 3:405462258899 | 191 | long sock = -1; |
frankvnk | 3:405462258899 | 192 | |
frankvnk | 3:405462258899 | 193 | sock = connect_to_exosite(); |
frankvnk | 3:405462258899 | 194 | if(sock == -1) return(sock); |
frankvnk | 3:405462258899 | 195 | |
frankvnk | 3:405462258899 | 196 | // This is an example write POST... |
frankvnk | 3:405462258899 | 197 | // s.send('POST /api:v1/stack/alias HTTP/1.1\r\n') |
frankvnk | 3:405462258899 | 198 | // s.send('Host: m2.exosite.com\r\n') |
frankvnk | 3:405462258899 | 199 | // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') |
frankvnk | 3:405462258899 | 200 | // s.send('Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n') |
frankvnk | 3:405462258899 | 201 | // s.send('Content-Length: 6\r\n\r\n') |
frankvnk | 3:405462258899 | 202 | // s.send('temp=2') |
frankvnk | 3:405462258899 | 203 | |
frankvnk | 3:405462258899 | 204 | sprintf(strBuf, "%d", (int)bufsize); //make a string for length |
frankvnk | 3:405462258899 | 205 | |
frankvnk | 3:405462258899 | 206 | sendLine(sock, POSTDATA_LINE, "/onep:v1/stack/alias"); |
frankvnk | 3:405462258899 | 207 | sendLine(sock, HOST_LINE, NULL); |
frankvnk | 3:405462258899 | 208 | sendLine(sock, CIK_LINE, NULL); |
frankvnk | 3:405462258899 | 209 | sendLine(sock, CONTENT_LINE, NULL); |
frankvnk | 3:405462258899 | 210 | sendLine(sock, LENGTH_LINE, strBuf); |
frankvnk | 3:405462258899 | 211 | //printf("DAta=%s\n", pbuf); |
frankvnk | 3:405462258899 | 212 | exoHAL_SocketSend(sock, pbuf, bufsize); //alias=value |
frankvnk | 3:405462258899 | 213 | |
frankvnk | 3:405462258899 | 214 | if (0 == readResponse(sock, "204")) |
frankvnk | 3:405462258899 | 215 | { |
frankvnk | 3:405462258899 | 216 | exositeWriteFailures = 0; |
frankvnk | 3:405462258899 | 217 | } |
frankvnk | 3:405462258899 | 218 | else |
frankvnk | 3:405462258899 | 219 | { |
frankvnk | 3:405462258899 | 220 | exositeWriteFailures++; |
frankvnk | 3:405462258899 | 221 | } |
frankvnk | 3:405462258899 | 222 | |
frankvnk | 3:405462258899 | 223 | exoHAL_SocketClose(sock); |
frankvnk | 3:405462258899 | 224 | |
frankvnk | 3:405462258899 | 225 | if (exositeWriteFailures > 5) |
frankvnk | 3:405462258899 | 226 | { |
frankvnk | 3:405462258899 | 227 | // sometimes transport connect works even if no connection... |
frankvnk | 3:405462258899 | 228 | exoHAL_HandleError(EXO_ERROR_WRITE); |
frankvnk | 3:405462258899 | 229 | } |
frankvnk | 3:405462258899 | 230 | |
frankvnk | 3:405462258899 | 231 | if (!exositeWriteFailures) |
frankvnk | 3:405462258899 | 232 | { |
frankvnk | 3:405462258899 | 233 | exoHAL_ShowUIMessage(EXO_CLIENT_RW); |
frankvnk | 3:405462258899 | 234 | return 0; // success |
frankvnk | 3:405462258899 | 235 | } |
frankvnk | 3:405462258899 | 236 | return 0; |
frankvnk | 3:405462258899 | 237 | } |
frankvnk | 3:405462258899 | 238 | |
frankvnk | 3:405462258899 | 239 | |
frankvnk | 3:405462258899 | 240 | //***************************************************************************** |
frankvnk | 3:405462258899 | 241 | // |
frankvnk | 3:405462258899 | 242 | //! Exosite_Read |
frankvnk | 3:405462258899 | 243 | //! |
frankvnk | 3:405462258899 | 244 | //! \param palias - string, name of the datasource alias to read from |
frankvnk | 3:405462258899 | 245 | //! pbuf - read buffer to put the read response into |
frankvnk | 3:405462258899 | 246 | //! buflen - size of the input buffer |
frankvnk | 3:405462258899 | 247 | //! |
frankvnk | 3:405462258899 | 248 | //! \return number of bytes read |
frankvnk | 3:405462258899 | 249 | //! |
frankvnk | 3:405462258899 | 250 | //! \brief The function reads data from Exosite |
frankvnk | 3:405462258899 | 251 | // |
frankvnk | 3:405462258899 | 252 | //***************************************************************************** |
frankvnk | 3:405462258899 | 253 | int Exosite_Read(char * palias, char * pbuf, unsigned char buflen) |
frankvnk | 3:405462258899 | 254 | { |
frankvnk | 3:405462258899 | 255 | unsigned char strLen, len, vlen; |
frankvnk | 3:405462258899 | 256 | char *p, *pcheck; |
frankvnk | 3:405462258899 | 257 | long sock = -1; |
frankvnk | 3:405462258899 | 258 | |
frankvnk | 3:405462258899 | 259 | while (sock < 0) |
frankvnk | 3:405462258899 | 260 | sock = connect_to_exosite(); |
frankvnk | 3:405462258899 | 261 | |
frankvnk | 3:405462258899 | 262 | // This is an example read GET |
frankvnk | 3:405462258899 | 263 | // s.send('GET /api:v1/stack/alias?temp HTTP/1.1\r\n') |
frankvnk | 3:405462258899 | 264 | // s.send('Host: m2.exosite.com\r\n') |
frankvnk | 3:405462258899 | 265 | // s.send('X-Exosite-CIK: 5046454a9a1666c3acfae63bc854ec1367167815\r\n') |
frankvnk | 3:405462258899 | 266 | // s.send('Accept: application/x-www-form-urlencoded; charset=utf-8\r\n\r\n') |
frankvnk | 3:405462258899 | 267 | |
frankvnk | 3:405462258899 | 268 | sendLine(sock, GETDATA_LINE, palias); |
frankvnk | 3:405462258899 | 269 | sendLine(sock, HOST_LINE, NULL); |
frankvnk | 3:405462258899 | 270 | sendLine(sock, CIK_LINE, NULL); |
frankvnk | 3:405462258899 | 271 | sendLine(sock, ACCEPT_LINE, "\r\n"); |
frankvnk | 3:405462258899 | 272 | |
frankvnk | 3:405462258899 | 273 | pcheck = palias; |
frankvnk | 3:405462258899 | 274 | vlen = 0; |
frankvnk | 3:405462258899 | 275 | |
frankvnk | 3:405462258899 | 276 | if (0 == readResponse(sock, "200")) |
frankvnk | 3:405462258899 | 277 | { |
frankvnk | 3:405462258899 | 278 | char strBuf[RX_SIZE]; |
frankvnk | 3:405462258899 | 279 | unsigned char crlf = 0; |
frankvnk | 3:405462258899 | 280 | exoHAL_ShowUIMessage(EXO_CLIENT_RW); |
frankvnk | 3:405462258899 | 281 | do |
frankvnk | 3:405462258899 | 282 | { |
frankvnk | 3:405462258899 | 283 | strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); |
frankvnk | 3:405462258899 | 284 | len = strLen; |
frankvnk | 3:405462258899 | 285 | p = strBuf; |
frankvnk | 3:405462258899 | 286 | |
frankvnk | 3:405462258899 | 287 | // Find 4 consecutive \r or \n - should be: \r\n\r\n |
frankvnk | 3:405462258899 | 288 | while (0 < len && 4 > crlf) |
frankvnk | 3:405462258899 | 289 | { |
frankvnk | 3:405462258899 | 290 | if ('\r' == *p || '\n' == *p) |
frankvnk | 3:405462258899 | 291 | { |
frankvnk | 3:405462258899 | 292 | ++crlf; |
frankvnk | 3:405462258899 | 293 | } |
frankvnk | 3:405462258899 | 294 | else |
frankvnk | 3:405462258899 | 295 | { |
frankvnk | 3:405462258899 | 296 | crlf = 0; |
frankvnk | 3:405462258899 | 297 | } |
frankvnk | 3:405462258899 | 298 | ++p; |
frankvnk | 3:405462258899 | 299 | --len; |
frankvnk | 3:405462258899 | 300 | } |
frankvnk | 3:405462258899 | 301 | |
frankvnk | 3:405462258899 | 302 | // The body is "<key>=<value>" |
frankvnk | 3:405462258899 | 303 | if (0 < len && 4 == crlf && buflen > vlen) |
frankvnk | 3:405462258899 | 304 | { |
frankvnk | 3:405462258899 | 305 | // Move past "<key>" |
frankvnk | 3:405462258899 | 306 | while (0 < len && 0 != *pcheck) |
frankvnk | 3:405462258899 | 307 | { |
frankvnk | 3:405462258899 | 308 | if (*pcheck == *p) |
frankvnk | 3:405462258899 | 309 | { |
frankvnk | 3:405462258899 | 310 | ++pcheck; |
frankvnk | 3:405462258899 | 311 | } |
frankvnk | 3:405462258899 | 312 | else |
frankvnk | 3:405462258899 | 313 | { |
frankvnk | 3:405462258899 | 314 | pcheck = palias; |
frankvnk | 3:405462258899 | 315 | } |
frankvnk | 3:405462258899 | 316 | ++p; |
frankvnk | 3:405462258899 | 317 | --len; |
frankvnk | 3:405462258899 | 318 | } |
frankvnk | 3:405462258899 | 319 | |
frankvnk | 3:405462258899 | 320 | // Match '=', we should now have '<key>=' |
frankvnk | 3:405462258899 | 321 | if (0 < len && 0 == *pcheck && '=' == *p) |
frankvnk | 3:405462258899 | 322 | { |
frankvnk | 3:405462258899 | 323 | ++p; |
frankvnk | 3:405462258899 | 324 | --len; |
frankvnk | 3:405462258899 | 325 | } |
frankvnk | 3:405462258899 | 326 | |
frankvnk | 3:405462258899 | 327 | // read in the rest of the body as the value |
frankvnk | 3:405462258899 | 328 | while (0 < len && buflen > vlen) |
frankvnk | 3:405462258899 | 329 | { |
frankvnk | 3:405462258899 | 330 | pbuf[vlen++] = *p++; |
frankvnk | 3:405462258899 | 331 | --len; |
frankvnk | 3:405462258899 | 332 | } |
frankvnk | 3:405462258899 | 333 | } |
frankvnk | 3:405462258899 | 334 | } while (RX_SIZE == strLen); |
frankvnk | 3:405462258899 | 335 | } |
frankvnk | 3:405462258899 | 336 | exoHAL_SocketClose(sock); |
frankvnk | 3:405462258899 | 337 | return vlen; |
frankvnk | 3:405462258899 | 338 | } |
frankvnk | 3:405462258899 | 339 | |
frankvnk | 3:405462258899 | 340 | |
frankvnk | 3:405462258899 | 341 | //***************************************************************************** |
frankvnk | 3:405462258899 | 342 | // |
frankvnk | 3:405462258899 | 343 | //! activate_device |
frankvnk | 3:405462258899 | 344 | //! |
frankvnk | 3:405462258899 | 345 | //! \param none |
frankvnk | 3:405462258899 | 346 | //! |
frankvnk | 3:405462258899 | 347 | //! \return none |
frankvnk | 3:405462258899 | 348 | //! |
frankvnk | 3:405462258899 | 349 | //! \brief Calls activation API - if successful, it saves the returned |
frankvnk | 3:405462258899 | 350 | //! CIK to non-volatile |
frankvnk | 3:405462258899 | 351 | // |
frankvnk | 3:405462258899 | 352 | //***************************************************************************** |
frankvnk | 3:405462258899 | 353 | void activate_device(void) |
frankvnk | 3:405462258899 | 354 | { |
frankvnk | 3:405462258899 | 355 | long sock = -1; |
frankvnk | 3:405462258899 | 356 | volatile int length; |
frankvnk | 3:405462258899 | 357 | char strLen[5]; |
frankvnk | 3:405462258899 | 358 | |
frankvnk | 3:405462258899 | 359 | while (sock < 0) |
frankvnk | 3:405462258899 | 360 | sock = connect_to_exosite(); |
frankvnk | 3:405462258899 | 361 | printf("Activating Device\n"); |
frankvnk | 3:405462258899 | 362 | |
frankvnk | 3:405462258899 | 363 | length = strlen(STR_VENDOR) + META_UUID_SIZE; |
frankvnk | 3:405462258899 | 364 | itoa(length, strLen, 10); //make a string for length |
frankvnk | 3:405462258899 | 365 | |
frankvnk | 3:405462258899 | 366 | sendLine(sock, POSTDATA_LINE, "/provision/activate"); |
frankvnk | 3:405462258899 | 367 | sendLine(sock, HOST_LINE, NULL); |
frankvnk | 3:405462258899 | 368 | sendLine(sock, CONTENT_LINE, NULL); |
frankvnk | 3:405462258899 | 369 | sendLine(sock, LENGTH_LINE, strLen); |
frankvnk | 3:405462258899 | 370 | sendLine(sock, VENDOR_LINE, NULL); |
frankvnk | 3:405462258899 | 371 | |
frankvnk | 3:405462258899 | 372 | if (0 == readResponse(sock, "200")) |
frankvnk | 3:405462258899 | 373 | { |
frankvnk | 3:405462258899 | 374 | char strBuf[RX_SIZE]; |
frankvnk | 3:405462258899 | 375 | unsigned char strLen, len; |
frankvnk | 3:405462258899 | 376 | char *p; |
frankvnk | 3:405462258899 | 377 | unsigned char crlf = 0; |
frankvnk | 3:405462258899 | 378 | unsigned char ciklen = 0; |
frankvnk | 3:405462258899 | 379 | char NCIK[CIK_LENGTH]; |
frankvnk | 3:405462258899 | 380 | |
frankvnk | 3:405462258899 | 381 | do |
frankvnk | 3:405462258899 | 382 | { |
frankvnk | 3:405462258899 | 383 | strLen = exoHAL_SocketRecv(sock, strBuf, RX_SIZE); |
frankvnk | 3:405462258899 | 384 | len = strLen; |
frankvnk | 3:405462258899 | 385 | p = strBuf; |
frankvnk | 3:405462258899 | 386 | // Find 4 consecutive \r or \n - should be: \r\n\r\n |
frankvnk | 3:405462258899 | 387 | while (0 < len && 4 > crlf) |
frankvnk | 3:405462258899 | 388 | { |
frankvnk | 3:405462258899 | 389 | if ('\r' == *p || '\n' == *p) |
frankvnk | 3:405462258899 | 390 | { |
frankvnk | 3:405462258899 | 391 | ++crlf; |
frankvnk | 3:405462258899 | 392 | } |
frankvnk | 3:405462258899 | 393 | else |
frankvnk | 3:405462258899 | 394 | { |
frankvnk | 3:405462258899 | 395 | crlf = 0; |
frankvnk | 3:405462258899 | 396 | } |
frankvnk | 3:405462258899 | 397 | ++p; |
frankvnk | 3:405462258899 | 398 | --len; |
frankvnk | 3:405462258899 | 399 | } |
frankvnk | 3:405462258899 | 400 | |
frankvnk | 3:405462258899 | 401 | // The body is the CIK |
frankvnk | 3:405462258899 | 402 | if (0 < len && 4 == crlf && CIK_LENGTH > ciklen) |
frankvnk | 3:405462258899 | 403 | { |
frankvnk | 3:405462258899 | 404 | // TODO, be more robust - match Content-Length header value to CIK_LENGTH |
frankvnk | 3:405462258899 | 405 | unsigned char need, part; |
frankvnk | 3:405462258899 | 406 | need = CIK_LENGTH - ciklen; |
frankvnk | 3:405462258899 | 407 | part = need < len ? need : len; |
frankvnk | 3:405462258899 | 408 | strncpy(NCIK + ciklen, p, part); |
frankvnk | 3:405462258899 | 409 | ciklen += part; |
frankvnk | 3:405462258899 | 410 | } |
frankvnk | 3:405462258899 | 411 | } while (RX_SIZE == strLen); |
frankvnk | 3:405462258899 | 412 | |
frankvnk | 3:405462258899 | 413 | if (CIK_LENGTH == ciklen) |
frankvnk | 3:405462258899 | 414 | { |
frankvnk | 3:405462258899 | 415 | Exosite_SetCIK(NCIK); |
frankvnk | 3:405462258899 | 416 | } |
frankvnk | 3:405462258899 | 417 | } |
frankvnk | 3:405462258899 | 418 | else |
frankvnk | 3:405462258899 | 419 | { |
frankvnk | 3:405462258899 | 420 | printf("Activation failed\n"); |
frankvnk | 3:405462258899 | 421 | getchar(); |
frankvnk | 3:405462258899 | 422 | } |
frankvnk | 3:405462258899 | 423 | exoHAL_SocketClose(sock); |
frankvnk | 3:405462258899 | 424 | } |
frankvnk | 3:405462258899 | 425 | |
frankvnk | 3:405462258899 | 426 | |
frankvnk | 3:405462258899 | 427 | //***************************************************************************** |
frankvnk | 3:405462258899 | 428 | // |
frankvnk | 3:405462258899 | 429 | //! update_m2ip |
frankvnk | 3:405462258899 | 430 | //! |
frankvnk | 3:405462258899 | 431 | //! \param none |
frankvnk | 3:405462258899 | 432 | //! |
frankvnk | 3:405462258899 | 433 | //! \return none |
frankvnk | 3:405462258899 | 434 | //! |
frankvnk | 3:405462258899 | 435 | //! \brief Checks /ip API to see if a new server IP address should be used |
frankvnk | 3:405462258899 | 436 | // |
frankvnk | 3:405462258899 | 437 | //***************************************************************************** |
frankvnk | 3:405462258899 | 438 | void update_m2ip(void) |
frankvnk | 3:405462258899 | 439 | { |
frankvnk | 3:405462258899 | 440 | //TODO - stubbed out |
frankvnk | 3:405462258899 | 441 | return; |
frankvnk | 3:405462258899 | 442 | } |
frankvnk | 3:405462258899 | 443 | |
frankvnk | 3:405462258899 | 444 | |
frankvnk | 3:405462258899 | 445 | //***************************************************************************** |
frankvnk | 3:405462258899 | 446 | // |
frankvnk | 3:405462258899 | 447 | //! init_mac_address |
frankvnk | 3:405462258899 | 448 | //! |
frankvnk | 3:405462258899 | 449 | //! \param Interface Number (1 - WiFi) |
frankvnk | 3:405462258899 | 450 | //! |
frankvnk | 3:405462258899 | 451 | //! \return None |
frankvnk | 3:405462258899 | 452 | //! |
frankvnk | 3:405462258899 | 453 | //! \brief Reads the MAC address from the hardware |
frankvnk | 3:405462258899 | 454 | // |
frankvnk | 3:405462258899 | 455 | //***************************************************************************** |
frankvnk | 3:405462258899 | 456 | void init_mac_address(unsigned char if_nbr) |
frankvnk | 3:405462258899 | 457 | { |
frankvnk | 3:405462258899 | 458 | const char hex[] = "0123456789abcdef"; |
frankvnk | 3:405462258899 | 459 | char strmac[18]; |
frankvnk | 3:405462258899 | 460 | unsigned char addr_hw[MAC_LEN]; |
frankvnk | 3:405462258899 | 461 | |
frankvnk | 3:405462258899 | 462 | exoHAL_ReadHWMAC(if_nbr, addr_hw); |
frankvnk | 3:405462258899 | 463 | printf("MAC=%08x\n", addr_hw); |
frankvnk | 3:405462258899 | 464 | strmac[0] = hex[addr_hw[0] >> 4]; |
frankvnk | 3:405462258899 | 465 | strmac[1] = hex[addr_hw[0] & 15]; |
frankvnk | 3:405462258899 | 466 | strmac[2] = ':'; |
frankvnk | 3:405462258899 | 467 | |
frankvnk | 3:405462258899 | 468 | strmac[3] = hex[addr_hw[1] >> 4]; |
frankvnk | 3:405462258899 | 469 | strmac[4] = hex[addr_hw[1] & 15]; |
frankvnk | 3:405462258899 | 470 | strmac[5] = ':'; |
frankvnk | 3:405462258899 | 471 | |
frankvnk | 3:405462258899 | 472 | strmac[6] = hex[addr_hw[2] >> 4]; |
frankvnk | 3:405462258899 | 473 | strmac[7] = hex[addr_hw[2] & 15]; |
frankvnk | 3:405462258899 | 474 | strmac[8] = ':'; |
frankvnk | 3:405462258899 | 475 | |
frankvnk | 3:405462258899 | 476 | strmac[9] = hex[addr_hw[3] >> 4]; |
frankvnk | 3:405462258899 | 477 | strmac[10] = hex[addr_hw[3] & 15]; |
frankvnk | 3:405462258899 | 478 | strmac[11] = ':'; |
frankvnk | 3:405462258899 | 479 | |
frankvnk | 3:405462258899 | 480 | strmac[12] = hex[addr_hw[4] >> 4]; |
frankvnk | 3:405462258899 | 481 | strmac[13] = hex[addr_hw[4] & 15]; |
frankvnk | 3:405462258899 | 482 | strmac[14] = ':'; |
frankvnk | 3:405462258899 | 483 | |
frankvnk | 3:405462258899 | 484 | strmac[15] = hex[addr_hw[5] >> 4]; |
frankvnk | 3:405462258899 | 485 | strmac[16] = hex[addr_hw[5] & 15]; |
frankvnk | 3:405462258899 | 486 | strmac[17] = NULL; |
frankvnk | 3:405462258899 | 487 | printf("\n\nMAC= %s\n\n", strmac); |
frankvnk | 3:405462258899 | 488 | exosite_meta_write((unsigned char *)strmac, 17, META_UUID); |
frankvnk | 3:405462258899 | 489 | } |
frankvnk | 3:405462258899 | 490 | |
frankvnk | 3:405462258899 | 491 | |
frankvnk | 3:405462258899 | 492 | //***************************************************************************** |
frankvnk | 3:405462258899 | 493 | // |
frankvnk | 3:405462258899 | 494 | //! connect_to_exosite |
frankvnk | 3:405462258899 | 495 | //! |
frankvnk | 3:405462258899 | 496 | //! \param None |
frankvnk | 3:405462258899 | 497 | //! |
frankvnk | 3:405462258899 | 498 | //! \return socket handle |
frankvnk | 3:405462258899 | 499 | //! |
frankvnk | 3:405462258899 | 500 | //! \brief Establishes a connection with the Exosite API server |
frankvnk | 3:405462258899 | 501 | // |
frankvnk | 3:405462258899 | 502 | //***************************************************************************** |
frankvnk | 3:405462258899 | 503 | long connect_to_exosite(void) |
frankvnk | 3:405462258899 | 504 | { |
frankvnk | 3:405462258899 | 505 | static unsigned char connectRetries = 0; |
frankvnk | 3:405462258899 | 506 | long sock; |
frankvnk | 3:405462258899 | 507 | |
frankvnk | 3:405462258899 | 508 | if (connectRetries++ > 5) |
frankvnk | 3:405462258899 | 509 | { |
frankvnk | 3:405462258899 | 510 | connectRetries = 0; |
frankvnk | 3:405462258899 | 511 | exoHAL_HandleError(EXO_ERROR_CONNECT); |
frankvnk | 3:405462258899 | 512 | } |
frankvnk | 3:405462258899 | 513 | |
frankvnk | 3:405462258899 | 514 | sock = exoHAL_SocketOpenTCP(); |
frankvnk | 3:405462258899 | 515 | //printf("socket=%d\n", sock); |
frankvnk | 3:405462258899 | 516 | if (sock == -1) |
frankvnk | 3:405462258899 | 517 | { |
frankvnk | 3:405462258899 | 518 | //wlan_stop(); //TODO - if we stop the wlan, we have to recover somehow... |
frankvnk | 3:405462258899 | 519 | exoHAL_MSDelay(100); |
frankvnk | 3:405462258899 | 520 | return -1; |
frankvnk | 3:405462258899 | 521 | } |
frankvnk | 3:405462258899 | 522 | |
frankvnk | 3:405462258899 | 523 | if (exoHAL_ServerConnect(sock) < 0) // Try to connect |
frankvnk | 3:405462258899 | 524 | { |
frankvnk | 3:405462258899 | 525 | // TODO - the typical reason the connect doesn't work is because |
frankvnk | 3:405462258899 | 526 | // something was wrong in the way the CC3000 was initialized (timing, bit |
frankvnk | 3:405462258899 | 527 | // error, etc...). There may be a graceful way to kick th CC3000 module |
frankvnk | 3:405462258899 | 528 | // back into gear at the right state, but for now, we just |
frankvnk | 3:405462258899 | 529 | // return and let the caller retry us if they want |
frankvnk | 3:405462258899 | 530 | printf("Connection to server failed\n"); |
frankvnk | 3:405462258899 | 531 | exoHAL_SocketClose(sock); |
frankvnk | 3:405462258899 | 532 | exoHAL_MSDelay(100); |
frankvnk | 3:405462258899 | 533 | return -1; |
frankvnk | 3:405462258899 | 534 | } |
frankvnk | 3:405462258899 | 535 | else |
frankvnk | 3:405462258899 | 536 | { |
frankvnk | 3:405462258899 | 537 | connectRetries = 0; |
frankvnk | 3:405462258899 | 538 | exoHAL_ShowUIMessage(EXO_SERVER_CONNECTED); |
frankvnk | 3:405462258899 | 539 | } |
frankvnk | 3:405462258899 | 540 | |
frankvnk | 3:405462258899 | 541 | // Success |
frankvnk | 3:405462258899 | 542 | //printf("success\n"); |
frankvnk | 3:405462258899 | 543 | return sock; |
frankvnk | 3:405462258899 | 544 | } |
frankvnk | 3:405462258899 | 545 | |
frankvnk | 3:405462258899 | 546 | |
frankvnk | 3:405462258899 | 547 | //***************************************************************************** |
frankvnk | 3:405462258899 | 548 | // |
frankvnk | 3:405462258899 | 549 | //! readResponse |
frankvnk | 3:405462258899 | 550 | //! |
frankvnk | 3:405462258899 | 551 | //! \param socket handle, pointer to expected HTTP response code |
frankvnk | 3:405462258899 | 552 | //! |
frankvnk | 3:405462258899 | 553 | //! \return 0 if match, -1 if no match |
frankvnk | 3:405462258899 | 554 | //! |
frankvnk | 3:405462258899 | 555 | //! \brief Reads first 12 bytes of HTTP response and extracts the 3 byte code |
frankvnk | 3:405462258899 | 556 | // |
frankvnk | 3:405462258899 | 557 | //***************************************************************************** |
frankvnk | 3:405462258899 | 558 | int readResponse(long socket, char * code) |
frankvnk | 3:405462258899 | 559 | { |
frankvnk | 3:405462258899 | 560 | char rxBuf[12]; |
frankvnk | 3:405462258899 | 561 | unsigned char rxLen; |
frankvnk | 3:405462258899 | 562 | rxLen = exoHAL_SocketRecv(socket, rxBuf, 12); |
frankvnk | 3:405462258899 | 563 | printf("rec %s\n", rxBuf); |
frankvnk | 3:405462258899 | 564 | if (12 == rxLen && code[0] == rxBuf[9] && code[1] == rxBuf[10] && code[2] == rxBuf[11]) |
frankvnk | 3:405462258899 | 565 | { |
frankvnk | 3:405462258899 | 566 | return 0; |
frankvnk | 3:405462258899 | 567 | } |
frankvnk | 3:405462258899 | 568 | |
frankvnk | 3:405462258899 | 569 | return -1; |
frankvnk | 3:405462258899 | 570 | } |
frankvnk | 3:405462258899 | 571 | |
frankvnk | 3:405462258899 | 572 | |
frankvnk | 3:405462258899 | 573 | //***************************************************************************** |
frankvnk | 3:405462258899 | 574 | // |
frankvnk | 3:405462258899 | 575 | //! sendLine |
frankvnk | 3:405462258899 | 576 | //! |
frankvnk | 3:405462258899 | 577 | //! \param Which line type |
frankvnk | 3:405462258899 | 578 | //! |
frankvnk | 3:405462258899 | 579 | //! \return socket handle |
frankvnk | 3:405462258899 | 580 | //! |
frankvnk | 3:405462258899 | 581 | //! \brief Sends data out the socket |
frankvnk | 3:405462258899 | 582 | // |
frankvnk | 3:405462258899 | 583 | //***************************************************************************** |
frankvnk | 3:405462258899 | 584 | void sendLine(long socket, unsigned char LINE, char * payload) |
frankvnk | 3:405462258899 | 585 | { |
frankvnk | 3:405462258899 | 586 | char strBuf[80]; |
frankvnk | 3:405462258899 | 587 | unsigned char strLen; |
frankvnk | 3:405462258899 | 588 | |
frankvnk | 3:405462258899 | 589 | switch(LINE) |
frankvnk | 3:405462258899 | 590 | { |
frankvnk | 3:405462258899 | 591 | case CIK_LINE: |
frankvnk | 3:405462258899 | 592 | sprintf(strBuf, "X-Exosite-CIK: %s\r\n", activeCIK); |
frankvnk | 3:405462258899 | 593 | strLen = strlen(strBuf); |
frankvnk | 3:405462258899 | 594 | //strLen = 57; |
frankvnk | 3:405462258899 | 595 | //memcpy(strBuf,STR_CIK_HEADER,strLen); |
frankvnk | 3:405462258899 | 596 | //exosite_meta_read((unsigned char *)&strBuf[strLen], CIK_LENGTH, META_CIK); |
frankvnk | 3:405462258899 | 597 | //strLen += CIK_LENGTH; |
frankvnk | 3:405462258899 | 598 | //memcpy(&strBuf[strLen],STR_CRLF, 2); |
frankvnk | 3:405462258899 | 599 | //strLen += 2; |
frankvnk | 3:405462258899 | 600 | break; |
frankvnk | 3:405462258899 | 601 | case HOST_LINE: |
frankvnk | 3:405462258899 | 602 | strLen = 28; |
frankvnk | 3:405462258899 | 603 | memcpy(strBuf,STR_HOST,strLen); |
frankvnk | 3:405462258899 | 604 | strBuf[strLen] = NULL; |
frankvnk | 3:405462258899 | 605 | break; |
frankvnk | 3:405462258899 | 606 | case CONTENT_LINE: |
frankvnk | 3:405462258899 | 607 | strLen = 64; |
frankvnk | 3:405462258899 | 608 | memcpy(strBuf,STR_CONTENT,strLen); |
frankvnk | 3:405462258899 | 609 | strBuf[strLen] = NULL; |
frankvnk | 3:405462258899 | 610 | break; |
frankvnk | 3:405462258899 | 611 | case ACCEPT_LINE: |
frankvnk | 3:405462258899 | 612 | strLen = 58; |
frankvnk | 3:405462258899 | 613 | memcpy(strBuf,STR_ACCEPT,strLen); |
frankvnk | 3:405462258899 | 614 | memcpy(&strBuf[strLen],payload, strlen(payload)); |
frankvnk | 3:405462258899 | 615 | strLen += strlen(payload); |
frankvnk | 3:405462258899 | 616 | break; |
frankvnk | 3:405462258899 | 617 | case LENGTH_LINE: // Content-Length: NN |
frankvnk | 3:405462258899 | 618 | strLen = 16; |
frankvnk | 3:405462258899 | 619 | memcpy(strBuf,STR_CONTENT_LENGTH,strLen); |
frankvnk | 3:405462258899 | 620 | memcpy(&strBuf[strLen],payload, strlen(payload)); |
frankvnk | 3:405462258899 | 621 | strLen += strlen(payload); |
frankvnk | 3:405462258899 | 622 | memcpy(&strBuf[strLen],STR_CRLF, 2); |
frankvnk | 3:405462258899 | 623 | strLen += 2; |
frankvnk | 3:405462258899 | 624 | memcpy(&strBuf[strLen],STR_CRLF, 2); |
frankvnk | 3:405462258899 | 625 | strLen += 2; |
frankvnk | 3:405462258899 | 626 | strBuf[strLen] = NULL; |
frankvnk | 3:405462258899 | 627 | break; |
frankvnk | 3:405462258899 | 628 | case GETDATA_LINE: |
frankvnk | 3:405462258899 | 629 | strLen = 24; |
frankvnk | 3:405462258899 | 630 | memcpy(strBuf,STR_GET_URL,strLen); |
frankvnk | 3:405462258899 | 631 | memcpy(&strBuf[strLen],payload, strlen(payload)); |
frankvnk | 3:405462258899 | 632 | strLen += strlen(payload); |
frankvnk | 3:405462258899 | 633 | memcpy(&strBuf[strLen],STR_HTTP, 12); |
frankvnk | 3:405462258899 | 634 | strLen += 12; |
frankvnk | 3:405462258899 | 635 | break; |
frankvnk | 3:405462258899 | 636 | case VENDOR_LINE: |
frankvnk | 3:405462258899 | 637 | strLen = strlen(STR_VENDOR); |
frankvnk | 3:405462258899 | 638 | memcpy(strBuf, STR_VENDOR, strLen); |
frankvnk | 3:405462258899 | 639 | exosite_meta_read((unsigned char *)&strBuf[strLen], META_UUID_SIZE, META_UUID); |
frankvnk | 3:405462258899 | 640 | strLen += META_UUID_SIZE; |
frankvnk | 3:405462258899 | 641 | strBuf[strLen] = NULL; |
frankvnk | 3:405462258899 | 642 | break; |
frankvnk | 3:405462258899 | 643 | case POSTDATA_LINE: |
frankvnk | 3:405462258899 | 644 | strLen = 5; |
frankvnk | 3:405462258899 | 645 | memcpy(strBuf,"POST ", strLen); |
frankvnk | 3:405462258899 | 646 | memcpy(&strBuf[strLen],payload, strlen(payload)); |
frankvnk | 3:405462258899 | 647 | strLen += strlen(payload); |
frankvnk | 3:405462258899 | 648 | memcpy(&strBuf[strLen],STR_HTTP, 12); |
frankvnk | 3:405462258899 | 649 | strLen += 12; |
frankvnk | 3:405462258899 | 650 | strBuf[strLen] = NULL; |
frankvnk | 3:405462258899 | 651 | break; |
frankvnk | 3:405462258899 | 652 | case EMPTY_LINE: |
frankvnk | 3:405462258899 | 653 | strLen = 2; |
frankvnk | 3:405462258899 | 654 | memcpy(strBuf,STR_CRLF,strLen); |
frankvnk | 3:405462258899 | 655 | break; |
frankvnk | 3:405462258899 | 656 | default: |
frankvnk | 3:405462258899 | 657 | break; |
frankvnk | 3:405462258899 | 658 | } |
frankvnk | 3:405462258899 | 659 | //printf("sendLine: %s\n", strBuf); |
frankvnk | 3:405462258899 | 660 | exoHAL_SocketSend(socket, strBuf, strLen); |
frankvnk | 3:405462258899 | 661 | return; |
frankvnk | 3:405462258899 | 662 | } |
frankvnk | 3:405462258899 | 663 |