Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of LinkNode-Test by
goosci_utility.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 "ble/BLE.h" 00018 #include "goosci_utility.h" 00019 #include "sensor.pb.h" 00020 #include "pb.h" 00021 #include "pb_decode.h" 00022 #include "pb_common.h" 00023 #include "mbed.h" 00024 #include "pb_encode.h" 00025 00026 00027 00028 goosci_SensorData sd = goosci_SensorData_init_zero; 00029 const int BUFFER_LEN = 256; 00030 uint8_t *buffer = NULL; 00031 pb_ostream_t stream; 00032 extern BLE ble; 00033 00034 00035 00036 int packets = 0; 00037 00038 void send_int_data(GattCharacteristic & characteristic, unsigned long timestamp_key, int value) 00039 { 00040 if ( buffer == NULL ) 00041 { 00042 buffer = (uint8_t *) malloc(BUFFER_LEN); 00043 } 00044 stream = pb_ostream_from_buffer(buffer, BUFFER_LEN); 00045 00046 goosci_SensorData sd = goosci_SensorData_init_zero; 00047 ; 00048 sd.timestamp_key = timestamp_key; // timestamp 00049 sd.which_result = (pb_size_t) goosci_SensorData_data_tag; 00050 sd.result.data.pin = goosci_Pin(); 00051 sd.result.data.pin.which_pin = goosci_Pin_analog_pin_tag; 00052 sd.result.data.pin.pin.analog_pin.pin = 0; 00053 sd.result.data.which_value = goosci_Data_analog_value_tag; 00054 sd.result.data.value.analog_value.value = value; 00055 00056 if (!pb_encode(&stream, goosci_SensorData_fields, &sd)) 00057 { 00058 //DEBUG_PRINT("Encoding failed: %s\n", PB_GET_ERROR(&stream)); 00059 } 00060 else 00061 { 00062 /* 00063 DEBUG_PRINT(F("send_data timestamp: ")); 00064 DEBUG_PRINTLN(sd.timestamp_key); 00065 DEBUG_PRINT(F("send_data value: ")); 00066 DEBUG_PRINTLN(value); 00067 DEBUG_PRINT(F("size: ")); 00068 DEBUG_PRINT(stream.bytes_written); 00069 DEBUG_PRINTLN(F(" bytes.")); 00070 String s; 00071 for (unsigned int i = 0; i < stream.bytes_written; ++i) { 00072 s += String(buffer[i], HEX); 00073 if ((i-1) % 2 == 0) s += " "; 00074 } 00075 DEBUG_PRINTLN(s.c_str()); 00076 */ 00077 uint8_t size = stream.bytes_written; 00078 const uint8_t max_packet_size = BTLE_BUFFER_SIZE - 2; 00079 /* Force size/max_packet_size to round up */ 00080 uint8_t num_packets = (size + max_packet_size - 1) / max_packet_size; 00081 00082 for (uint8_t ii = 0; ii < num_packets; ii++) 00083 { 00084 bool is_last_packet = ((num_packets - 1) == ii); 00085 /* There are 3 possibilities for current_packet_size 00086 1) It is the last packet and the remaining data is smaller than our allocated buffer (size % max_packet_size) 00087 2) It is the last packet and the remaining data is equal to our allocated buffer (max_packet_size) 00088 3) It is not the last packet (max_packet_size) 00089 */ 00090 uint8_t current_packet_size = 00091 (is_last_packet ? 00092 ((size % max_packet_size == 0) ? 00093 max_packet_size : (size % max_packet_size)) : 00094 max_packet_size); 00095 00096 uint8_t packet[BTLE_BUFFER_SIZE]; 00097 packet[0] = current_packet_size; 00098 packet[1] = is_last_packet; 00099 memcpy((void*) (packet + 2), buffer + ii * max_packet_size,current_packet_size); 00100 00101 00102 if(!ble.updateCharacteristicValue(characteristic.getValueAttribute().getHandle(), packet,current_packet_size + 2)) 00103 { 00104 //DEBUG_PRINTLN("Send of packet failed."); 00105 break; 00106 } 00107 } 00108 } 00109 } 00110 00111 //cann't send float data . 00112 void send_float_data(GattCharacteristic & characteristic, unsigned long timestamp_key, float fvalue) 00113 { 00114 if ( buffer == NULL ) 00115 { 00116 buffer = (uint8_t *) malloc(BUFFER_LEN); 00117 } 00118 stream = pb_ostream_from_buffer(buffer, BUFFER_LEN); 00119 00120 goosci_SensorData sd = goosci_SensorData_init_zero; 00121 ; 00122 sd.timestamp_key = timestamp_key; // timestamp 00123 sd.which_result = (pb_size_t) goosci_SensorData_data_tag; 00124 sd.result.data.pin = goosci_Pin(); 00125 sd.result.data.pin.which_pin = goosci_Pin_analog_pin_tag; 00126 sd.result.data.pin.pin.analog_pin.pin = 0; 00127 sd.result.data.which_value = goosci_Data_float_value_tag; 00128 sd.result.data.value.float_value.value = fvalue; 00129 00130 if (!pb_encode(&stream, goosci_SensorData_fields, &sd)) 00131 { 00132 //DEBUG_PRINT("Encoding failed: %s\n", PB_GET_ERROR(&stream)); 00133 } 00134 else 00135 { 00136 /* 00137 DEBUG_PRINT(F("send_data timestamp: ")); 00138 DEBUG_PRINTLN(sd.timestamp_key); 00139 DEBUG_PRINT(F("send_data value: ")); 00140 DEBUG_PRINTLN(value); 00141 DEBUG_PRINT(F("size: ")); 00142 DEBUG_PRINT(stream.bytes_written); 00143 DEBUG_PRINTLN(F(" bytes.")); 00144 String s; 00145 for (unsigned int i = 0; i < stream.bytes_written; ++i) { 00146 s += String(buffer[i], HEX); 00147 if ((i-1) % 2 == 0) s += " "; 00148 } 00149 DEBUG_PRINTLN(s.c_str()); 00150 */ 00151 uint8_t size = stream.bytes_written; 00152 const uint8_t max_packet_size = BTLE_BUFFER_SIZE - 2; 00153 /* Force size/max_packet_size to round up */ 00154 uint8_t num_packets = (size + max_packet_size - 1) / max_packet_size; 00155 00156 for (uint8_t ii = 0; ii < num_packets; ii++) 00157 { 00158 bool is_last_packet = ((num_packets - 1) == ii); 00159 /* There are 3 possibilities for current_packet_size 00160 1) It is the last packet and the remaining data is smaller than our allocated buffer (size % max_packet_size) 00161 2) It is the last packet and the remaining data is equal to our allocated buffer (max_packet_size) 00162 3) It is not the last packet (max_packet_size) 00163 */ 00164 uint8_t current_packet_size = 00165 (is_last_packet ? 00166 ((size % max_packet_size == 0) ? 00167 max_packet_size : (size % max_packet_size)) : 00168 max_packet_size); 00169 00170 uint8_t packet[BTLE_BUFFER_SIZE]; 00171 packet[0] = current_packet_size; 00172 packet[1] = is_last_packet; 00173 memcpy((void*) (packet + 2), buffer + ii * max_packet_size,current_packet_size); 00174 00175 00176 if(!ble.updateCharacteristicValue(characteristic.getValueAttribute().getHandle(), packet,current_packet_size + 2)) 00177 { 00178 //DEBUG_PRINTLN("Send of packet failed."); 00179 break; 00180 } 00181 } 00182 } 00183 } 00184 00185 00186 00187 bool encode_pin(pb_ostream_t *stream, const pb_field_t *field,void * const *arg) 00188 { 00189 goosci_Pin sp = goosci_Pin_init_zero; 00190 sp.pin.analog_pin.pin = 0; 00191 if (!pb_encode_tag_for_field(stream, field)) 00192 return false; 00193 if (!pb_encode_submessage(stream, goosci_Pin_fields, &sp)) 00194 return false; 00195 return true; 00196 }
Generated on Tue Jul 12 2022 16:00:20 by
1.7.2
