Using BLE to control WIFI configuration as SSID and PW.

Dependencies:   BLE_API WIFI_API_32kRAM mbed nRF51822

Fork of NNN40_WiFi by Delta

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BLEControlWIFIService.h Source File

BLEControlWIFIService.h

00001 #ifndef __BLE_HEART_RATE_SERVICE_H__
00002 #define __BLE_HEART_RATE_SERVICE_H__
00003 
00004 #include "BLE.h"
00005 #include "WIFIDevice.h"
00006 
00007 static const uint8_t UUID_WIFI_CONFIGURATION_SERVICE[] = { 0x44, 0x38, 0x70, 0x22, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
00008 static const uint8_t UUID_CONNECTION_STATUS_CHAR[] = { 0x44, 0x38, 0x73, 0x7e, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
00009 static const uint8_t UUID_SSID_INFO_CHAR[] = { 0x44, 0x38, 0x75, 0xd6, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
00010 static const uint8_t UUID_NETWORK_SECURITY_PASSWORD_CHAR[] = { 0x44, 0x38, 0x78, 0x1a, 0x40, 0x02, 0x11, 0xe4, 0x85, 0x14, 0x16, 0x42, 0x30, 0xd1, 0xdf, 0x67 };
00011 
00012 static const uint8_t MAX_PAYLOAD_SIZE = 20;
00013 
00014 class BLEControlWIFIService {
00015 public:
00016     bool is_config;
00017     char *ssid;
00018     uint8_t uint_ssid[20];
00019     char *pw;
00020     uint8_t uint_pw[20];
00021 
00022 public:
00023 
00024     BLEControlWIFIService(BLEDevice &_ble, WIFIDevice &_wifi) :
00025         ble(_ble),
00026         wifi(_wifi),
00027         is_config(false),
00028         statusCharacteristic(UUID_CONNECTION_STATUS_CHAR, statusPayload, 1, MAX_PAYLOAD_SIZE,
00029                                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00030         ssidCharacteristic (UUID_SSID_INFO_CHAR, ssidPayload, 1, MAX_PAYLOAD_SIZE,
00031                                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
00032         pwCharacteristic (UUID_NETWORK_SECURITY_PASSWORD_CHAR, statusPayload, 1, MAX_PAYLOAD_SIZE,
00033                                       GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ)
00034     {
00035         setupService();
00036     }
00037 
00038 
00039     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00040             uint8_t rssi, i;
00041             uint16_t bytesRead = params->len;         
00042             if (params->handle == statusCharacteristic.getValueAttribute().getHandle()) {
00043                 memcpy(statusPayload, params->data, bytesRead);
00044         
00045                 ble.updateCharacteristicValue(statusCharacteristic.getValueAttribute().getHandle(), statusPayload, bytesRead);
00046             } else if (params->handle == ssidCharacteristic.getValueAttribute().getHandle()) {
00047                 rssi = ssidPayload[0];
00048                 for(i=1; i<bytesRead; i++)
00049                     uint_ssid[i-1] = params->data[i];
00050                     uint_ssid[bytesRead-1] = '\0';   
00051                     ssid = (char*) uint_ssid; 
00052                     memcpy(ssidPayload, params->data, bytesRead);
00053 
00054                     ssidPayload[0] = rssi;      // Added by Tsungta, restore RSSI value
00055                     ble.updateCharacteristicValue(ssidCharacteristic.getValueAttribute().getHandle(), ssidPayload, bytesRead);
00056             } else if (params->handle == pwCharacteristic.getValueAttribute().getHandle()) {
00057                 for(i=1; i<bytesRead; i++)
00058                     uint_pw[i-1] = params->data[i];
00059                     uint_pw[bytesRead-1] = '\0';
00060                     pw = (char*) uint_pw;
00061                     memcpy(pwPayload, params->data, bytesRead);
00062                     ble.updateCharacteristicValue(pwCharacteristic.getValueAttribute().getHandle(), pwPayload, bytesRead);
00063                     wifi.setNetwork(ssid, pw, 0);
00064                     is_config = true;
00065             }
00066     }
00067 
00068 
00069 private:
00070     void setupService(void) {
00071         static bool serviceAdded = false; /* We should only ever need to add the heart rate service once. */
00072         if (serviceAdded) {
00073             return;
00074         }
00075 
00076         GattCharacteristic *charTable[] = {&statusCharacteristic, &ssidCharacteristic, &pwCharacteristic};
00077         GattService         BLEWIFIService(UUID_WIFI_CONFIGURATION_SERVICE, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00078 
00079         ble.addService(BLEWIFIService);
00080         serviceAdded = true;
00081 
00082         ble.onDataWritten(this, &BLEControlWIFIService::onDataWritten);
00083     }
00084 
00085 
00086 private:
00087     BLEDevice               &ble;
00088     WIFIDevice              &wifi;
00089     uint8_t statusPayload[MAX_PAYLOAD_SIZE];
00090     uint8_t ssidPayload[MAX_PAYLOAD_SIZE];
00091     uint8_t pwPayload[MAX_PAYLOAD_SIZE];
00092     
00093     GattCharacteristic                      statusCharacteristic;
00094     GattCharacteristic                      ssidCharacteristic;
00095     GattCharacteristic                      pwCharacteristic;
00096 };
00097 
00098 #endif /* #ifndef __BLE_HEART_RATE_SERVICE_H__*/