This is an example of BLE GATT Client, which receives broadcast data from BLE_Server_BME280 ( a GATT server) , then transfers values up to mbed Device Connector (cloud).

Please refer details about BLEClient_mbedDevConn below. https://github.com/soramame21/BLEClient_mbedDevConn

The location of required BLE GATT server, BLE_Server_BME280, is at here. https://developer.mbed.org/users/edamame22/code/BLE_Server_BME280/

Committer:
Ren Boting
Date:
Tue Sep 05 11:56:13 2017 +0900
Revision:
2:b894b3508057
Parent:
0:29983394c6b6
Update all libraries and reform main.cpp

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:29983394c6b6 1 /*
edamame22 0:29983394c6b6 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
edamame22 0:29983394c6b6 3 * SPDX-License-Identifier: Apache-2.0
edamame22 0:29983394c6b6 4 * Licensed under the Apache License, Version 2.0 (the License); you may
edamame22 0:29983394c6b6 5 * not use this file except in compliance with the License.
edamame22 0:29983394c6b6 6 * You may obtain a copy of the License at
edamame22 0:29983394c6b6 7 *
edamame22 0:29983394c6b6 8 * http://www.apache.org/licenses/LICENSE-2.0
edamame22 0:29983394c6b6 9 *
edamame22 0:29983394c6b6 10 * Unless required by applicable law or agreed to in writing, software
edamame22 0:29983394c6b6 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
edamame22 0:29983394c6b6 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
edamame22 0:29983394c6b6 13 * See the License for the specific language governing permissions and
edamame22 0:29983394c6b6 14 * limitations under the License.
edamame22 0:29983394c6b6 15 */
edamame22 0:29983394c6b6 16 #include <string.h>
edamame22 0:29983394c6b6 17 #include <stdarg.h>
edamame22 0:29983394c6b6 18 #include <stdio.h>
edamame22 0:29983394c6b6 19
edamame22 0:29983394c6b6 20 #include "ns_types.h"
edamame22 0:29983394c6b6 21 #include "eventOS_event.h"
edamame22 0:29983394c6b6 22 #include "eventOS_event_timer.h"
edamame22 0:29983394c6b6 23 #include "net_interface.h"
edamame22 0:29983394c6b6 24 #include "socket_api.h"
edamame22 0:29983394c6b6 25 #include "mbed-trace/mbed_trace.h"
edamame22 0:29983394c6b6 26 #include "common_functions.h"
edamame22 0:29983394c6b6 27
edamame22 0:29983394c6b6 28 #include "ns_cmdline.h"
edamame22 0:29983394c6b6 29 #include "cmd_commands.h"
edamame22 0:29983394c6b6 30 #include "cmd_lwm2m.h"
edamame22 0:29983394c6b6 31
edamame22 0:29983394c6b6 32 #define TRACE_GROUP "cApp"
edamame22 0:29983394c6b6 33
edamame22 0:29983394c6b6 34 #define ESC 0x03
edamame22 0:29983394c6b6 35 #define EVENT_DATA_COMMAND_EXECUTED 6
edamame22 0:29983394c6b6 36
edamame22 0:29983394c6b6 37 // Prototypes
edamame22 0:29983394c6b6 38 void cmd_ready_cb(int retcode);
edamame22 0:29983394c6b6 39
edamame22 0:29983394c6b6 40 static void cmdline_event(arm_event_s *event);
edamame22 0:29983394c6b6 41
edamame22 0:29983394c6b6 42 /**
edamame22 0:29983394c6b6 43 * Callback function for thread handling.
edamame22 0:29983394c6b6 44 */
edamame22 0:29983394c6b6 45 void* __thread_poll_function(void*);
edamame22 0:29983394c6b6 46
edamame22 0:29983394c6b6 47 typedef struct {
edamame22 0:29983394c6b6 48 uint8_t tasklet_id;
edamame22 0:29983394c6b6 49 } cmd_commands_t;
edamame22 0:29983394c6b6 50
edamame22 0:29983394c6b6 51 cmd_commands_t cmd_commands;
edamame22 0:29983394c6b6 52 pthread_t input_thread; /* Thread for input_terminal-function */
edamame22 0:29983394c6b6 53
edamame22 0:29983394c6b6 54 void initialize_app_commands(int8_t /*rf_driver_id*/)
edamame22 0:29983394c6b6 55 {
edamame22 0:29983394c6b6 56 //initialize ready cb
edamame22 0:29983394c6b6 57 cmd_set_ready_cb( cmd_ready_cb );
edamame22 0:29983394c6b6 58 lwm2m_command_init();
edamame22 0:29983394c6b6 59
edamame22 0:29983394c6b6 60 cmd_commands.tasklet_id = eventOS_event_handler_create(&cmdline_event, EVENT_TYPE_CMDLINE);
edamame22 0:29983394c6b6 61 pthread_create(&input_thread, NULL,__thread_poll_function, NULL);
edamame22 0:29983394c6b6 62 }
edamame22 0:29983394c6b6 63
edamame22 0:29983394c6b6 64 void cmd_ready_cb(int retcode)
edamame22 0:29983394c6b6 65 {
edamame22 0:29983394c6b6 66 tr_debug("cmd_ready_cb(%d)", retcode);
edamame22 0:29983394c6b6 67 arm_event_s event;
edamame22 0:29983394c6b6 68 event.sender = cmd_commands.tasklet_id;
edamame22 0:29983394c6b6 69 event.receiver = cmd_commands.tasklet_id;
edamame22 0:29983394c6b6 70 event.event_type = APPLICATION_EVENT;
edamame22 0:29983394c6b6 71 event.event_id = EVENT_DATA_COMMAND_EXECUTED;
edamame22 0:29983394c6b6 72 event.event_data = retcode;
edamame22 0:29983394c6b6 73 eventOS_event_send(&event);
edamame22 0:29983394c6b6 74 }
edamame22 0:29983394c6b6 75
edamame22 0:29983394c6b6 76 static void cmdline_event(arm_event_s *event)
edamame22 0:29983394c6b6 77 {
edamame22 0:29983394c6b6 78 switch( event->event_type )
edamame22 0:29983394c6b6 79 {
edamame22 0:29983394c6b6 80 case ARM_LIB_TASKLET_INIT_EVENT:
edamame22 0:29983394c6b6 81 //tasklet up and running
edamame22 0:29983394c6b6 82 tr_warning("cmdline_event-ARM_LIB_TASKLET_INIT_EVENT");
edamame22 0:29983394c6b6 83 break;
edamame22 0:29983394c6b6 84
edamame22 0:29983394c6b6 85 case APPLICATION_EVENT:
edamame22 0:29983394c6b6 86 if( event->event_id == EVENT_DATA_COMMAND_EXECUTED )
edamame22 0:29983394c6b6 87 {
edamame22 0:29983394c6b6 88 int retcode = event->event_data;
edamame22 0:29983394c6b6 89 cmd_next( retcode );
edamame22 0:29983394c6b6 90 }
edamame22 0:29983394c6b6 91 break;
edamame22 0:29983394c6b6 92 default:
edamame22 0:29983394c6b6 93 tr_warning("Unknown event type (type: %i, id: %i)", event->event_type, event->event_id);
edamame22 0:29983394c6b6 94 break;
edamame22 0:29983394c6b6 95 }
edamame22 0:29983394c6b6 96 }
edamame22 0:29983394c6b6 97
edamame22 0:29983394c6b6 98 void* __thread_poll_function(void*)
edamame22 0:29983394c6b6 99 {
edamame22 0:29983394c6b6 100 int16_t c = getchar();
edamame22 0:29983394c6b6 101 while( c >= 0 ) {
edamame22 0:29983394c6b6 102 cmd_char_input(c);
edamame22 0:29983394c6b6 103 c = getchar();
edamame22 0:29983394c6b6 104 }
edamame22 0:29983394c6b6 105 return NULL;
edamame22 0:29983394c6b6 106 }