Qi Yao / LinkNode---test123

Dependencies:   mbed

Fork of LinkNode-Test by Qi Yao

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers config_change.cpp Source File

config_change.cpp

00001 /*
00002  *  Copyright 2016 Google Inc. All Rights Reserved.
00003  *
00004  *  Licensed under the Apache License, Version 2.0 (the "License");
00005  *  you may not use this file except in compliance with the License.
00006  *  You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  *  Unless required by applicable law or agreed to in writing, software
00011  *  distributed under the License is distributed on an "AS IS" BASIS,
00012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *  See the License for the specific language governing permissions and
00014  *  limitations under the License.
00015  */
00016 
00017 #include <stdlib.h>
00018 #include "config_change.h"
00019 
00020 #define SENSOR_CONFIG_SIZE 20
00021 
00022 PinType pin_type = ANALOG;
00023 int pin = 0;
00024 static uint8_t sensorConfig[SENSOR_CONFIG_SIZE];
00025 
00026 bool decode_pin(pb_istream_t *stream, const pb_field_t *field, void * *arg) {
00027     goosci_Pin sp = goosci_Pin_init_zero;
00028     if (!pb_decode(stream, goosci_Pin_fields, &sp))
00029         return false;
00030 
00031     if (sp.which_pin == goosci_Pin_analog_pin_tag) {
00032         pin_type = ANALOG;
00033         pin = sp.pin.analog_pin.pin;
00034 
00035     } 
00036     else if (sp.which_pin == goosci_Pin_digital_pin_tag) 
00037     {
00038         pin_type = DIGITAL;
00039         pin = sp.pin.digital_pin.pin;
00040 
00041     } 
00042     else 
00043     {
00044         pin_type = VIRTUAL;
00045     }
00046     return true;
00047 }
00048 
00049 void handle(uint8_t* data, int8_t length) {
00050     int8_t size = data[0];
00051     bool last = data[1] == 1;
00052     if (last) {
00053         uint8_t sensorConfig[SENSOR_CONFIG_SIZE];
00054         memcpy(sensorConfig, data + 2, size);
00055         pb_istream_t stream = pb_istream_from_buffer(sensorConfig, size);
00056         goosci_SensorDataRequest sdr = goosci_SensorDataRequest_init_zero;
00057         sdr.pin.funcs.decode = &decode_pin;
00058         if (!pb_decode(&stream, goosci_SensorDataRequest_fields, &sdr)) {
00059             //DEBUG_PRINT("Failed parse of string.");
00060         }
00061     }
00062 
00063 }