Commit 22b3d6de authored by apapkovskiy's avatar apapkovskiy
Browse files

Fixed several bugs

parent 548acfae
......@@ -36,7 +36,12 @@ namespace rigado {
static struct hci_filter original_filter;
static int device_handle;
static size_t calcDecodeLength(const char* b64input) { //Calculates the length of a decoded string
/**
* @brief Calculates the length of a decoded string
*
* @param[in] b64input Base64 encoded string
*/
static size_t calcDecodeLength(const char* b64input) {
size_t len = strlen(b64input),
padding = 0;
......@@ -48,7 +53,14 @@ namespace rigado {
return (len*3)/4 - padding;
}
static int Base64Decode(const char* b64message, unsigned char** buffer, size_t* length) { //Decodes a base64 encoded string
/**
* @brief Decodes a base64 encoded string
*
* @param[in] b64message Base64 encoded string
* @param[out] buffer Pointer to buffer for decoded data. This buffer is allocated inside the function
* @param[out] length Length of decoded data
*/
static int Base64Decode(const char* b64message, unsigned char** buffer, size_t* length) {
BIO *bio, *b64;
size_t decodeLen = calcDecodeLength(b64message);
......@@ -72,19 +84,35 @@ namespace rigado {
return (0); //success
}
/**
* @brief Parses the PIR sensor's CBOR message to get the movement count
*
* @param[in] p_buf CBOR message
*/
static int cbor_PIR_parser(const uint8_t *p_buf){
// Example - A3:1:19:33:2C:2:A:18:2C:00
const uint8_t control_buf[] = { 0xA3, 0x01, 0x19, 0x33, 0x2C, 0x02, 0x0A, 0x18, 0x2C };
// Check the const part of the message
if(memcmp(p_buf, control_buf, sizeof(control_buf)) != 0){
return -1;
}
if(p_buf[sizeof(control_buf)] == 24){
return ((uint32_t)p_buf[sizeof(control_buf)] << 8) | (uint32_t)p_buf[sizeof(control_buf) + 1 ];
if( p_buf[sizeof(control_buf) ] == 24){
return (uint32_t)p_buf[ sizeof(control_buf) + 1 ];
} else if(p_buf[sizeof(control_buf)] == 25){
return ((uint32_t)p_buf[ sizeof(control_buf) + 1 ] << 8) | (uint32_t)p_buf[ sizeof(control_buf) + 2 ];
}
return p_buf[sizeof(control_buf)];
return p_buf[ sizeof(control_buf) ];
}
static int cmd_up(int device_handle, int dev_ind)
/**
* @brief Up hci device
*
* @param[in] device_handle Socket handle of HCI device
* @param[in] dev_ind Index of HCI device (0 for hci0, 1 for hci1 ...)
* @return 0 if successful, -1 otherwise
*/
static int ble_hci_up(int device_handle, int dev_ind)
{
/* Start HCI device */
if (ioctl(device_handle, HCIDEVUP, dev_ind) < 0) {
......@@ -95,7 +123,15 @@ namespace rigado {
}
return 0;
}
static int cmd_down(int device_handle, int dev_ind)
/**
* @brief Down hci device
*
* @param[in] device_handle Socket handle of HCI device
* @param[in] dev_ind Index of HCI device (0 for hci0, 1 for hci1 ...)
* @return 0 if successful, -1 otherwise
*/
static int ble_hci_down(int device_handle, int dev_ind)
{
/* Stop HCI device */
if (ioctl(device_handle, HCIDEVDOWN, dev_ind) < 0) {
......@@ -105,6 +141,11 @@ namespace rigado {
return 0;
}
/**
* @brief Start BLE scan
*
* @param[in] cb Callback object to handle the JSON message created
*/
int ble_start(ble_scanner_cb_t cb){
socklen_t olen = sizeof(original_filter);
......@@ -117,8 +158,8 @@ namespace rigado {
return -1;
}
if (cmd_down(device_handle, BLE_DEFAULT_HCI) ||
cmd_up(device_handle, BLE_DEFAULT_HCI)){
if (ble_hci_down(device_handle, BLE_DEFAULT_HCI) ||
ble_hci_up(device_handle, BLE_DEFAULT_HCI)){
goto ble_start_error;
}
......@@ -186,21 +227,23 @@ namespace rigado {
}
index += info->data[index] + 1;
}
// Check this is the PIR sensor's adv packet
if(memcmp(device_name, "~ow", 3) == 0){
uint8_t *p_buf64;
size_t len64;
std::stringstream ss;
// Skip '~' - device_name + 1
// To skip '~' - device_name + 1
Base64Decode(device_name + 1, &p_buf64, &len64);
for(size_t i = 0; i < len64; i++){
printf("%X:", p_buf64[i]);
}
printf("\n");
ss << "{ sensors: [ {sensorMACAddr: \"" << addr << "\"," <<std::endl;
ss << "movements: " << cbor_PIR_parser(p_buf64) << "," <<std::endl;
ss << "RSSI: " << dev_RSSI << "}]}";
// Create JSON messsage
ss << "{ \"sensors\": [ {\"sensorMACAddr\": \"" << addr << "\"," <<std::endl;
ss << "\"movements\": " << cbor_PIR_parser(p_buf64) << "," <<std::endl;
ss << "\"RSSI\": " << dev_RSSI << "}]}";
free(p_buf64);
AWS_LOG_DEBUG(LOG_TAG_BLE, ss.str().c_str());
......@@ -209,7 +252,6 @@ namespace rigado {
goto ble_start_error;
}
}
offset = info->data + info->length + 2;
}
}
......@@ -225,6 +267,9 @@ ble_start_error:
return -1;
}
/**
* @brief Stops BLE scan
*/
int ble_stop(void){
setsockopt(device_handle, SOL_HCI, HCI_FILTER, &original_filter, sizeof(original_filter));
if(hci_le_set_scan_enable(device_handle, 0x00, 1, 1000) < 0)
......
......@@ -8,6 +8,7 @@ sudo snap connect a71ch-aws-client:network-control :network-control
sudo snap connect a71ch-aws-client:network-setup-control :network-setup-control
sudo snap connect a71ch-aws-client:lib-engine rigado-hsm-server:lib-engine
sudo snap connect a71ch-aws-client:lib-cert rigado-hsm-server:lib-cert
sudo snap connect a71ch-aws-client:bluetooth-control :bluetooth-control
```
## Configuration the JRCP connection
......@@ -51,27 +52,18 @@ sudo snap set a71ch-aws-client a71ch-do-provisioning=DONT_PROVISIONING
```
## The main workflow
1. Install rigado-hsm-server snap
1. Install rigado-hsm-server snap in devmode
2. Install a71ch-aws-client snap
3. !!! Very important. Check in the AWS account if there is a thing corresponding to the pre-provisioning device ID, delete it if it present
4. Set up pre-provisioning A71CH
5. Check publishing messages
## The current workflow (with some manual stuff)
The messages have JSON format and followed structure
{
"sensors": [ {
"sensorMACAddr": "XX:XX:XX:XX:XX:XX",
"movements": XX,
"RSSI":XX
}]
}
Currently we are using only device with ID 04007002ffff00000000BB00000000000003 and NXP AWS demo account!!!
1. Install rigado-hsm-server snap
2. Install a71ch-aws-client snap
3. Stop pub-sub-sample service
```
sudo snap stop a71ch-aws-client.pub-sub-sample
```
4. Set up pre-provisioning A71CH
5. !!! Very important. Check in the AWS account if there is a thing corresponding to the pre-provisioning device ID, delete it if it present
6. Provision A71CH
7. Open autogenerated file $SNAP_COMMON/certs/SampleConfig.json and replace (this example is done for deviceID 04007002ffff00000000BB00000000000003, change the last digit for another deviceID):
```
- "client_id": "LightingDevices",
+ "client_id": "LightingDevices_348598032839380244606242920424862583357443",
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment