MeshCtl - Mesh GATT Client Provisioner
Subsystems (3)
cover MeshCtl - Mesh GATT Client Provisioner
With the recent adoption of the Bluetooth Mesh Profile, we offer this GATT Client provisioner tool (meshctl).
Brian Gix / Inga Stotland` (5):
mesh: Add BT SIG reserved numbers for Mesh
mesh: define APIs for Bluetooth Mesh
mesh: Baseline Mesh runtime configuration files
mesh: Baseline Mesh implementation
mesh: Add mesh to main bluez build
Makefile.tools | 24 + bootstrap-configure | 1 + configure.ac | 18 + lib/uuid.h | 10 + mesh/README | 26 + mesh/agent.c | 276 ++++++ mesh/agent.h | 58 ++ mesh/config-client.c | 667 +++++++++++++++ mesh/config-model.h | 119 +++ mesh/config-server.c | 165 ++++ mesh/crypto.c | 1168 ++++++++++++++++++++++++++ mesh/crypto.h | 133 +++ mesh/gatt.c | 609 ++++++++++++++ mesh/gatt.h | 47 ++ mesh/keys.h | 43 + mesh/local_node.json | 61 ++ mesh/main.c | 2269 ++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/mesh-net.h | 174 ++++ mesh/net.c | 2184 ++++++++++++++++++++++++++++++++++++++++++++++++ mesh/net.h | 72 ++ mesh/node.c | 879 +++++++++++++++++++ mesh/node.h | 146 ++++ mesh/onoff-model.c | 306 +++++++ mesh/onoff-model.h | 50 ++ mesh/prov-db.c | 1599 +++++++++++++++++++++++++++++++++++ mesh/prov-db.h | 52 ++ mesh/prov.c | 664 +++++++++++++++ mesh/prov.h | 43 + mesh/prov_db.json | 37 + mesh/util.c | 369 ++++++++ mesh/util.h | 71 ++ 31 files changed, 12340 insertions(+) create mode 100644 mesh/README create mode 100644 mesh/agent.c create mode 100644 mesh/agent.h create mode 100644 mesh/config-client.c create mode 100644 mesh/config-model.h create mode 100644 mesh/config-server.c create mode 100644 mesh/crypto.c create mode 100644 mesh/crypto.h create mode 100644 mesh/gatt.c create mode 100644 mesh/gatt.h create mode 100644 mesh/keys.h create mode 100644 mesh/local_node.json create mode 100644 mesh/main.c create mode 100644 mesh/mesh-net.h create mode 100644 mesh/net.c create mode 100644 mesh/net.h create mode 100644 mesh/node.c create mode 100644 mesh/node.h create mode 100644 mesh/onoff-model.c create mode 100644 mesh/onoff-model.h create mode 100644 mesh/prov-db.c create mode 100644 mesh/prov-db.h create mode 100644 mesh/prov.c create mode 100644 mesh/prov.h create mode 100644 mesh/prov_db.json create mode 100644 mesh/util.c create mode 100644 mesh/util.h
Signature · 1 line
2.9.5
PATCH 1/5 mesh: Add BT SIG reserved numbers for Mesh
--- lib/uuid.h | 10 ++++++++++ 1 file changed, 10 insertions(+)
lib/uuid.h +10 -0
@@ -139,6 +139,16 @@ extern "C" {#define GATT_EXTERNAL_REPORT_REFERENCE 0x2907#define GATT_REPORT_REFERENCE 0x2908 +/* GATT Mesh Services */ +#define MESH_PROV_SVC_UUID "00001827-0000-1000-8000-00805f9b34fb" +#define MESH_PROXY_SVC_UUID "00001828-0000-1000-8000-00805f9b34fb" + +/* GATT Mesh Characteristic Types */ +#define MESH_PROVISIONING_DATA_IN 0x2ADB +#define MESH_PROVISIONING_DATA_OUT 0x2ADC +#define MESH_PROXY_DATA_IN 0x2ADD +#define MESH_PROXY_DATA_OUT 0x2ADE + typedef struct { enum { BT_UUID_UNSPEC = 0,
Signature · 1 line
2.9.5
PATCH 2/5 mesh: define APIs for Bluetooth Mesh
In reply to Brian Gix--- mesh/README | 26 ++++++++ mesh/agent.h | 58 ++++++++++++++++++ mesh/config-model.h | 119 +++++++++++++++++++++++++++++++++++ mesh/crypto.h | 133 +++++++++++++++++++++++++++++++++++++++ mesh/gatt.h | 47 ++++++++++++++ mesh/keys.h | 43 +++++++++++++ mesh/mesh-net.h | 174 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/net.h | 72 ++++++++++++++++++++++ mesh/node.h | 146 +++++++++++++++++++++++++++++++++++++++++++ mesh/onoff-model.h | 50 +++++++++++++++ mesh/prov-db.h | 52 ++++++++++++++++ mesh/prov.h | 43 +++++++++++++ mesh/util.h | 71 +++++++++++++++++++++ 13 files changed, 1034 insertions(+) create mode 100644 mesh/README create mode 100644 mesh/agent.h create mode 100644 mesh/config-model.h create mode 100644 mesh/crypto.h create mode 100644 mesh/gatt.h create mode 100644 mesh/keys.h create mode 100644 mesh/mesh-net.h create mode 100644 mesh/net.h create mode 100644 mesh/node.h create mode 100644 mesh/onoff-model.h create mode 100644 mesh/prov-db.h create mode 100644 mesh/prov.h create mode 100644 mesh/util.h
mesh/README new file +26 -0
@@ -0,0 +1,26 @@ +MeshCtl - BlueZ GATT based Bluetooth Mesh Provisioner +****************************************** + +Copyright (C) 2017 Intel Corporation. All rights reserved. + +Compilation and installation +============================ + +In addition to main BlueZ requirements, MeshCtl needs the following: + - JSON library + +Configuration and options +========================= + + --enable-mesh + + Build meshctl and other Bluetooth Mesh based tools and utils + +Information +=========== + +Mailing lists: + [email protected] + +For additional information about the project visit BlueZ web site: + http://www.bluez.org
mesh/agent.h new file +58 -0
58 lines not on this page · raw message
mesh/config-model.h new file +119 -0
119 lines not on this page · raw message
mesh/crypto.h new file +133 -0
133 lines not on this page · raw message
mesh/gatt.h new file +47 -0
47 lines not on this page · raw message
mesh/keys.h new file +43 -0
43 lines not on this page · raw message
mesh/mesh-net.h new file +174 -0
174 lines not on this page · raw message
mesh/net.h new file +72 -0
72 lines not on this page · raw message
mesh/node.h new file +146 -0
146 lines not on this page · raw message
mesh/onoff-model.h new file +50 -0
50 lines not on this page · raw message
mesh/prov-db.h new file +52 -0
52 lines not on this page · raw message
mesh/prov.h new file +43 -0
43 lines not on this page · raw message
mesh/util.h new file +71 -0
71 lines not on this page · raw message
Signature · 1 line
2.9.5
PATCH 3/5 mesh: Baseline Mesh runtime configuration files
In reply to Brian Gix--- mesh/local_node.json | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/prov_db.json | 37 +++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 mesh/local_node.json create mode 100644 mesh/prov_db.json
mesh/local_node.json new file +61 -0
@@ -0,0 +1,61 @@ +{ + "$schema":"file:\/\/\/Bluetooth SIG\/Mesh\/schema\/mesh.jsonschema", + "meshName":"Intel BT Mesh", + "netKeys":[ + { + "index": 0, + "keyRefresh": 0 + } + ], + "appKeys":[ + { + "index": 0, + "boundNetKey": 0 + }, + { + "index": 1, + "boundNetKey": 0 + } + ], +"node": { + "IVindex":"00000005", + "IVupdate":"0", + "sequenceNumber": 0, + "composition": { + "cid": "0002", + "pid": "0010", + "vid": "0001", + "crpl": "000a", + "features": { + "relay": false, + "proxy": true, + "friend": false, + "lowPower": false + }, + "elements": [ + { + "elementIndex": 0, + "location": "0001", + "models": ["0000", "0001", "1001"] + } + ] + }, + "configuration":{ + "netKeys": [0], + "appKeys": [ 0, 1], + "defaultTTL": 10, + "elements": [ + { + "elementIndex": 0, + "unicastAddress":"0077", + "models": [ + { + "modelId": "1001", + "bind": [1] + } + ] + } + ] + } + } +}
mesh/prov_db.json new file +37 -0
@@ -0,0 +1,37 @@ +{ + "$schema":"file:\/\/\/Bluetooth SIG\/Mesh\/schema\/mesh.jsonschema", + "meshName":"Intel BT Mesh", + "IVindex":5, + "IVupdate":0, + "netKeys":[ + { + "index":0, + "keyRefresh":0, + "key":"18eed9c2a56add85049ffc3c59ad0e12" + } + ], + "appKeys":[ + { + "index":0, + "boundNetKey":0, + "key":"4f68ad85d9f48ac8589df665b6b49b8a" + }, + { + "index":1, + "boundNetKey":0, + "key":"2aa2a6ded5a0798ceab5787ca3ae39fc" + } + ], + "provisioners":[ + { + "provisionerName":"BT Mesh Provisioner", + "unicastAddress":"0077", + "allocatedUnicastRange":[ + { + "lowAddress":"0100", + "highAddress":"7fff" + } + ] + } + ], +}
Signature · 1 line
2.9.5
PATCH 4/5 mesh: Baseline Mesh implementation
In reply to Brian Gix--- mesh/agent.c | 276 ++++++ mesh/config-client.c | 667 +++++++++++++++ mesh/config-server.c | 165 ++++ mesh/crypto.c | 1168 ++++++++++++++++++++++++++ mesh/gatt.c | 609 ++++++++++++++ mesh/main.c | 2269 ++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/net.c | 2184 ++++++++++++++++++++++++++++++++++++++++++++++++ mesh/node.c | 879 +++++++++++++++++++ mesh/onoff-model.c | 306 +++++++ mesh/prov-db.c | 1599 +++++++++++++++++++++++++++++++++++ mesh/prov.c | 664 +++++++++++++++ mesh/util.c | 369 ++++++++ 12 files changed, 11155 insertions(+) create mode 100644 mesh/agent.c create mode 100644 mesh/config-client.c create mode 100644 mesh/config-server.c create mode 100644 mesh/crypto.c create mode 100644 mesh/gatt.c create mode 100644 mesh/main.c create mode 100644 mesh/net.c create mode 100644 mesh/node.c create mode 100644 mesh/onoff-model.c create mode 100644 mesh/prov-db.c create mode 100644 mesh/prov.c create mode 100644 mesh/util.c
mesh/agent.c new file +276 -0
@@ -0,0 +1,276 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <inttypes.h> +#include <readline/readline.h> + +#include <glib.h> + +#include <lib/bluetooth.h> +#include "client/display.h" +#include "util.h" +#include "agent.h" + +#define AGENT_PROMPT COLOR_RED "[agent]" COLOR_OFF " " + +static char *agent_saved_prompt =NULL ; +static int agent_saved_point =0 ; + +struct input_request { +oob_type_t type; +uint16_t len; +agent_input_cb cb; +void *user_data; +}; + +static struct input_request pending_request = {NONE ,0 ,NULL ,NULL }; + +static void agent_prompt (const char *msg) +{ +char *prompt; + +/* Normal use should not prompt for user input to the agent a second +* time before it releases the prompt, but we take a safe action. */ +if (agent_saved_prompt) +return ; + + agent_saved_point = rl_point; + agent_saved_prompt =g_strdup (rl_prompt); + +rl_set_prompt ("" ); +rl_redisplay (); + + prompt =g_strdup_printf (AGENT_PROMPT "%s" , msg); +rl_set_prompt (prompt); +g_free (prompt); + +rl_replace_line ("" ,0 ); +rl_redisplay (); +} + +static void agent_release_prompt (void ) +{ +if (!agent_saved_prompt) +return ; + +/* This will cause rl_expand_prompt to re-run over the last prompt, but +* our prompt doesn't expand anyway. */ +rl_set_prompt (agent_saved_prompt); +rl_replace_line ("" ,0 ); + rl_point = agent_saved_point; +rl_redisplay (); + +g_free (agent_saved_prompt); + agent_saved_prompt =NULL ; +} + +bool agent_completion (void ) +{ +if (pending_request.type ==NONE ) +return false; + +return true; +} + +static bool response_hexadecimal (const char *input) +{ +uint8_t buf[MAX_HEXADECIMAL_OOB_LEN ]; + +if (!str2hex (input,strlen (input), buf, pending_request.len) ) { +rl_printf ("Incorrect input: expecting %d hex octets\n" , + pending_request.len); +return false; + } + +if (pending_request.cb) + pending_request.cb (HEXADECIMAL , buf, pending_request.len, + pending_request.user_data); +return true; +} + +static bool response_decimal (const char *input) +{ +uint8_t buf[DECIMAL_OOB_LEN ]; + +if (strlen (input) > pending_request.len) +return false; + +bt_put_be32 (atoi (input), buf); + +if (pending_request.cb) + pending_request.cb (DECIMAL , buf,DECIMAL_OOB_LEN , + pending_request.user_data); + +return true; +} + +static void response_ascii (const char *input) +{ +if (pending_request.cb) + pending_request.cb (ASCII , (uint8_t *) input,strlen (input), + pending_request.user_data); +} + +bool agent_input (const char *input) +{ +bool repeat = false; + +if (pending_request.type ==NONE ) +return false; + +switch (pending_request.type) { +case HEXADECIMAL : +if (!response_hexadecimal (input)) + repeat = true; +break ; +case DECIMAL : +if (!response_decimal (input)) + repeat = true; +break ; +case ASCII : +response_ascii (input); +break ; +case OUTPUT : + repeat = true; +case NONE : +default : +break ; + }; + +if (!repeat) { + pending_request.type =NONE ; + pending_request.len =0 ; + pending_request.cb =NULL ; + pending_request.user_data =NULL ; + +agent_release_prompt (); + } + +return true; +} + +void agent_release (void ) +{ +agent_release_prompt (); +} + +static bool request_hexadecimal (uint16_t len) +{ +if (len >MAX_HEXADECIMAL_OOB_LEN ) +return false; + +rl_printf ("Request hexadecimal key (hex %d octets)\n" , len); +agent_prompt ("Enter key (hex number): " ); + +return true; +} + +static uint32_t power_ten (uint8_t power) +{ +uint32_t ret =1 ; + +while (power--) + ret *=10 ; + +return ret; +} + +static bool request_decimal (uint16_t len) +{ +rl_printf ("Request decimal key (0 - %d)\n" ,power_ten (len) -1 ); +agent_prompt ("Enter Numeric key: " ); + +return true; +} + +static bool request_ascii (uint16_t len) +{ +if (len !=MAX_ASCII_OOB_LEN ) +return false; + +rl_printf ("Request ASCII key (max characters %d)\n" , len); +agent_prompt ("Enter key (ascii string): " ); + +return true; +} + +bool agent_input_request (oob_type_t type,uint16_t max_len,agent_input_cb cb, +void *user_data) +{ +bool result; + +if (pending_request.type !=NONE ) +return FALSE; + +switch (type) { +case HEXADECIMAL : + result =request_hexadecimal (max_len); +break ; +case DECIMAL : + result =request_decimal (max_len); +break ; +case ASCII : + result =request_ascii (max_len); +break ; +case NONE : +case OUTPUT : +default : +return false; + }; + +if (result) { + pending_request.type = type; + pending_request.len = max_len; + pending_request.cb = cb; + pending_request.user_data = user_data; + +return true; + } + +return false; +} + +bool agent_output_request (const char * str) +{ +if (pending_request.type !=NONE ) +return false; + + pending_request.type =OUTPUT ; +agent_prompt (str); +return true; +} + +void agent_output_request_cancel (void ) +{ +if (pending_request.type !=OUTPUT ) +return ; + pending_request.type =NONE ; +agent_release_prompt (); +}
mesh/config-client.c new file +667 -0
667 lines not on this page · raw message
mesh/config-server.c new file +165 -0
165 lines not on this page · raw message
mesh/crypto.c new file +1168 -0
1,168 lines not on this page · raw message
mesh/gatt.c new file +609 -0
609 lines not on this page · raw message
mesh/main.c new file +2269 -0
115 lines not on this page · raw message
mesh/net.c new file +2184 -0
2,184 lines not on this page · raw message
mesh/node.c new file +879 -0
879 lines not on this page · raw message
mesh/onoff-model.c new file +306 -0
306 lines not on this page · raw message
mesh/prov-db.c new file +1599 -0
1,599 lines not on this page · raw message
mesh/prov.c new file +664 -0
664 lines not on this page · raw message
mesh/util.c new file +369 -0
369 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.9.5
Part of the diff is not shown here. View the raw message.
Luiz Augusto von Dentz
maintainerRe: [PATCH 4/5] mesh: Baseline Mesh implementation
Hi Brian,
Quoted 299 lines · Brian Gix
--- mesh/agent.c | 276 ++++++ mesh/config-client.c | 667 +++++++++++++++ mesh/config-server.c | 165 ++++ mesh/crypto.c | 1168 ++++++++++++++++++++++++++ mesh/gatt.c | 609 ++++++++++++++ mesh/main.c | 2269 ++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/net.c | 2184 ++++++++++++++++++++++++++++++++++++++++++++++++ mesh/node.c | 879 +++++++++++++++++++ mesh/onoff-model.c | 306 +++++++ mesh/prov-db.c | 1599 +++++++++++++++++++++++++++++++++++ mesh/prov.c | 664 +++++++++++++++ mesh/util.c | 369 ++++++++ 12 files changed, 11155 insertions(+) create mode 100644 mesh/agent.c create mode 100644 mesh/config-client.c create mode 100644 mesh/config-server.c create mode 100644 mesh/crypto.c create mode 100644 mesh/gatt.c create mode 100644 mesh/main.c create mode 100644 mesh/net.c create mode 100644 mesh/node.c create mode 100644 mesh/onoff-model.c create mode 100644 mesh/prov-db.c create mode 100644 mesh/prov.c create mode 100644 mesh/util.cmesh/agent.c new file +266
-0@@ -0,0 +1,276 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <inttypes.h> +#include <readline/readline.h> + +#include <glib.h> + +#include <lib/bluetooth.h> +#include "client/display.h" +#include "util.h" +#include "agent.h" + +#define AGENT_PROMPT COLOR_RED "[agent]" COLOR_OFF " " + +static char *agent_saved_prompt =NULL ; +static int agent_saved_point =0 ; + +struct input_request { +oob_type_t type; +uint16_t len; +agent_input_cb cb; +void *user_data; +}; + +static struct input_request pending_request = {NONE ,0 ,NULL ,NULL }; + +static void agent_prompt (const char *msg) +{ +char *prompt; + +/* Normal use should not prompt for user input to the agent a second +* time before it releases the prompt, but we take a safe action. */ +if (agent_saved_prompt) +return ; + + agent_saved_point = rl_point; + agent_saved_prompt =g_strdup (rl_prompt); + +rl_set_prompt ("" ); +rl_redisplay (); + + prompt =g_strdup_printf (AGENT_PROMPT "%s" , msg); +rl_set_prompt (prompt); +g_free (prompt); + +rl_replace_line ("" ,0 ); +rl_redisplay (); +} + +static void agent_release_prompt (void ) +{ +if (!agent_saved_prompt) +return ; + +/* This will cause rl_expand_prompt to re-run over the last prompt, but +* our prompt doesn't expand anyway. */ +rl_set_prompt (agent_saved_prompt); +rl_replace_line ("" ,0 ); + rl_point = agent_saved_point; +rl_redisplay (); + +g_free (agent_saved_prompt); + agent_saved_prompt =NULL ; +} + +bool agent_completion (void ) +{ +if (pending_request.type ==NONE ) +return false; + +return true; +} + +static bool response_hexadecimal (const char *input) +{ +uint8_t buf[MAX_HEXADECIMAL_OOB_LEN ]; + +if (!str2hex (input,strlen (input), buf, pending_request.len) ) { +rl_printf ("Incorrect input: expecting %d hex octets\n" , + pending_request.len); +return false; + } + +if (pending_request.cb) + pending_request.cb (HEXADECIMAL , buf, pending_request.len, + pending_request.user_data); +return true; +} + +static bool response_decimal (const char *input) +{ +uint8_t buf[DECIMAL_OOB_LEN ]; + +if (strlen (input) > pending_request.len) +return false; + +bt_put_be32 (atoi (input), buf); + +if (pending_request.cb) + pending_request.cb (DECIMAL , buf,DECIMAL_OOB_LEN , + pending_request.user_data); + +return true; +} + +static void response_ascii (const char *input) +{ +if (pending_request.cb) + pending_request.cb (ASCII , (uint8_t *) input,strlen (input), + pending_request.user_data); +} + +bool agent_input (const char *input) +{ +bool repeat = false; + +if (pending_request.type ==NONE ) +return false; + +switch (pending_request.type) { +case HEXADECIMAL : +if (!response_hexadecimal (input)) + repeat = true; +break ; +case DECIMAL : +if (!response_decimal (input)) + repeat = true; +break ; +case ASCII : +response_ascii (input); +break ; +case OUTPUT : + repeat = true; +case NONE : +default : +break ; + }; + +if (!repeat) { + pending_request.type =NONE ; + pending_request.len =0 ; + pending_request.cb =NULL ; + pending_request.user_data =NULL ; + +agent_release_prompt (); + } + +return true; +} + +void agent_release (void ) +{ +agent_release_prompt (); +} + +static bool request_hexadecimal (uint16_t len) +{ +if (len >MAX_HEXADECIMAL_OOB_LEN ) +return false; + +rl_printf ("Request hexadecimal key (hex %d octets)\n" , len); +agent_prompt ("Enter key (hex number): " ); + +return true; +} + +static uint32_t power_ten (uint8_t power) +{ +uint32_t ret =1 ; + +while (power--) + ret *=10 ; + +return ret; +} + +static bool request_decimal (uint16_t len) +{ +rl_printf ("Request decimal key (0 - %d)\n" ,power_ten (len) -1 ); +agent_prompt ("Enter Numeric key: " ); + +return true; +} + +static bool request_ascii (uint16_t len) +{ +if (len !=MAX_ASCII_OOB_LEN ) +return false; + +rl_printf ("Request ASCII key (max characters %d)\n" , len); +agent_prompt ("Enter key (ascii string): " ); + +return true; +} + +bool agent_input_request (oob_type_t type,uint16_t max_len,agent_input_cb cb, +void *user_data) +{ +bool result; + +if (pending_request.type !=NONE ) +return FALSE; + +switch (type) { +case HEXADECIMAL : + result =request_hexadecimal (max_len); +break ; +case DECIMAL : + result =request_decimal (max_len); +break ; +case ASCII : + result =request_ascii (max_len); +break ; +case NONE : +case OUTPUT : +default : +return false; + }; + +if (result) { + pending_request.type = type; + pending_request.len = max_len; + pending_request.cb = cb; + pending_request.user_data = user_data; + +return true; + } + +return false; +} + +bool agent_output_request (const char * str) +{ +if (pending_request.type !=NONE ) +return false; + + pending_request.type =OUTPUT ; +agent_prompt (str);
mesh/agent.c line 276
Quoted 10 lines
mesh/agent.c new file +10
-0+return true; +} + +void agent_output_request_cancel (void ) +{ +if (pending_request.type !=OUTPUT ) +return ; + pending_request.type =NONE ; +agent_release_prompt (); +}
The whole agent.c can be removed as well.
Quoted 10,948 lines
mesh/config-client.c new file +667
-0@@ -0,0 +1,667 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdbool.h> +#include <inttypes.h> +#include <stdbool.h> +#include <sys/uio.h> +#include <wordexp.h> +#include <readline/readline.h> +#include <readline/history.h> +#include <glib.h> + +#include "src/shared/util.h" +#include "client/display.h" +#include "mesh-net.h" +#include "keys.h" +#include "net.h" +#include "node.h" +#include "prov-db.h" +#include "util.h" +#include "config-model.h" + +#define MIN_COMPOSITION_LEN 16 + +static bool client_msg_recvd (uint16_t src,uint8_t *data, +uint16_t len,void *user_data) +{ +uint32_t opcode; +struct mesh_node *node; +uint16_t app_idx, net_idx, addr; +uint32_t mod_id; +uint16_t primary; +uint16_t ele_addr; +uint8_t ele_idx; +struct mesh_publication pub; +int n; + +if (mesh_opcode_get (data, len, &opcode, &n)) { + len -= n; + data += n; + }else +return false; + +if (IS_UNICAST (src)) { + node =node_find_by_addr (src); + }else + node =NULL ; + +if (!node) +return false; + + primary =node_get_primary (node); +if (primary != src) +return false; + +switch (opcode & ~OP_UNRELIABLE ) { +default : +return false; + +case OP_DEV_COMP_STATUS : +if (len <MIN_COMPOSITION_LEN || !node) +break ; +if (node_parse_composition (node, data, len)) { +if (!prov_db_add_node_composition (node, data, len)) +break ; + } + +if (node_get_composition (node)) +prov_db_print_node_composition (node); +break ; + +case OP_APPKEY_STATUS : +if (len !=4 ) +break ; + +rl_printf ("Node %4.4x AppKey Status %s\n" , src, +mesh_status_str (data[0 ])); + net_idx =get_le16 (data +1 ) &0xfff ; + app_idx =get_le16 (data +2 ) >>4 ; + +rl_printf ("\tNetKey %3.3x, AppKey %3.3x\n" , net_idx, app_idx); + +if (data[0 ] !=MESH_STATUS_SUCCESS && + data[0 ] !=MESH_STATUS_IDX_ALREADY_STORED && +node_app_key_delete (node, net_idx, app_idx)) +prov_db_node_keys (node,node_get_app_keys (node), +"appKeys" ); +break ; + +case OP_NETKEY_STATUS : +if (len !=3 ) +break ; + +rl_printf ("Node %4.4x NetKey Status %s\n" , src, +mesh_status_str (data[0 ])); + net_idx =get_le16 (data +1 ) &0xfff ; + +rl_printf ("\tNetKey %3.3x\n" , net_idx); + +if (data[0 ] !=MESH_STATUS_SUCCESS && + data[0 ] !=MESH_STATUS_IDX_ALREADY_STORED && +node_net_key_delete (node, net_idx)) +prov_db_node_keys (node,node_get_net_keys (node), +"netKeys" ); +break ; + +case OP_MODEL_APP_STATUS : +if (len !=7 && len !=9 ) +break ; + +rl_printf ("Node %4.4x Model App Status %s\n" , src, +mesh_status_str (data[0 ])); + addr =get_le16 (data +1 ); + app_idx =get_le16 (data +3 ); + +rl_printf ("\tElement %4.4x AppIdx %3.3x\n " , addr, app_idx); + +if (len ==7 ) { + mod_id =get_le16 (data +5 ); +rl_printf ("ModelId %4.4x\n" , mod_id); + mod_id =0xffff0000 | mod_id; + }else { + mod_id =get_le16 (data +7 ); +rl_printf ("ModelId %4.4x %4.4x\n" ,get_le16 (data +5 ), + mod_id); + mod_id =get_le16 (data +5 ) <<16 | mod_id; + } + +if (data[0 ] ==MESH_STATUS_SUCCESS && +node_add_binding (node, addr - src, mod_id, app_idx)) +prov_db_add_binding (node, addr - src, mod_id, app_idx); +break ; + +case OP_CONFIG_DEFAULT_TTL_STATUS : +if (len !=1 ) +return true; +rl_printf ("Node %4.4x Default TTL %d\n" , src, data[0 ]); +if (node_set_default_ttl (node, data[0 ])) +prov_db_node_set_ttl (node, data[0 ]); +break ; + +case OP_CONFIG_MODEL_PUB_STATUS : +if (len !=12 && len !=14 ) +return true; + +rl_printf ("\nSet publication for node %4.4x status: %s\n" , src, + data[0 ] ==MESH_STATUS_SUCCESS ?"Success" : +mesh_status_str (data[0 ])); + +if (data[0 ] !=MESH_STATUS_SUCCESS ) +return true; + + ele_addr =get_le16 (data +1 ); + mod_id =get_le16 (data +10 ); +if (len ==14 ) + mod_id = (mod_id <<16 ) |get_le16 (data +12 ); +else + mod_id |=0xffff0000 ; + + pub.u.addr16 =get_le16 (data +3 ); + pub.app_idx =get_le16 (data +5 ); + pub.ttl = data[7 ]; + pub.period = data[8 ]; + n = (data[8 ] &0x3f ); +switch (data[8 ] >>6 ) { +case 0 : +rl_printf ("Period: %d ms\n" , n *100 ); +break ; +case 2 : + n *=10 ; +/* fall through */ +case 1 : +rl_printf ("Period: %d sec\n" , n); +break ; +case 3 : +rl_printf ("Period: %d min\n" , n *10 ); +break ; + } + + pub.retransmit = data[9 ]; +rl_printf ("Retransmit count: %d\n" , data[9 ] >>5 ); +rl_printf ("Retransmit Interval Steps: %d\n" , data[9 ] &0x1f ); + + ele_idx = ele_addr -node_get_primary (node); + +/* Local configuration is saved by server */ +if (node ==node_get_local_node ()) +break ; + +if (node_model_pub_set (node, ele_idx, mod_id, &pub)) +prov_db_node_set_model_pub (node, ele_idx, mod_id, +node_model_pub_get (node, ele_idx, mod_id)); +break ; + } +return true; +} + +static uint32_t target; +static uint32_t parms[8 ]; + +static uint32_t read_input_parameters (const char *args) +{ +uint32_t i; + +if (!args) +return 0 ; + +memset (parms,0xff ,sizeof (parms)); + +for (i =0 ; i <sizeof (parms)/sizeof (parms[0 ]); i++) { +int n; + +sscanf (args,"%x" , &parms[i]); +if (parms[i] ==0xffffffff ) +break ; + + n =strcspn (args," \t" ); + args = args + n +strspn (args + n," \t" ); + } + +return i; +} + +static void cmd_set_node (const char *args) +{ +uint32_t dst; +char *end; + + dst =strtol (args, &end,16 ); +if (end != (args +4 )) { +rl_printf ("Bad unicast address %s: " +"expected format 4 digit hex\n" , args); + target =UNASSIGNED_ADDRESS ; + }else { +rl_printf ("Configuring node %4.4x\n" , dst); + target = dst; +set_menu_prompt ("config" , args); + } + +} + +static bool config_send (uint8_t *buf,uint16_t len) +{ +struct mesh_node *node =node_get_local_node (); +uint16_t primary; + +if (!node) +return false; + + primary =node_get_primary (node); +if (target != primary) +return net_access_layer_send (DEFAULT_TTL , primary, + target,APP_IDX_DEV , buf, len); + +node_local_data_handler (primary, target,node_get_iv_index (node), +node_get_sequence_number (node),APP_IDX_DEV , + buf, len); +return true; + +} + +static void cmd_get_composition (const char *args) +{ +uint16_t n; +uint8_t msg[32 ]; +struct mesh_node *node; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + node =node_find_by_addr (target); + +if (!node) +return ; + + n =mesh_opcode_set (OP_DEV_COMP_GET , msg); + +/* By default, use page 0 */ + msg[n++] = (read_input_parameters (args) ==1 ) ? parms[0 ] :0 ; + +if (!config_send (msg, n)) +rl_printf ("Failed to send \"GET NODE COMPOSITION\"\n" ); +} + +static void cmd_net_key (const char *args,uint32_t opcode) +{ +uint16_t n; +uint8_t msg[32 ]; +uint16_t net_idx; +uint8_t *key; +struct mesh_node *node; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + n =mesh_opcode_set (opcode, msg); + +if (read_input_parameters (args) !=1 ) { +rl_printf ("Bad arguments %s\n" , args); +return ; + } + + node =node_find_by_addr (target); +if (!node) { +rl_printf ("Node %4.4x\n not found" , target); +return ; + } + + net_idx = parms[0 ]; + +if (opcode !=OP_NETKEY_DELETE ) { + + key =keys_net_key_get (net_idx, true); +if (!key) { +rl_printf ("Network key with index %4.4x not found\n" , + net_idx); +return ; + } + +put_le16 (net_idx, &msg[n]); + n +=2 ; + +memcpy (msg + n, key,16 ); + n +=16 ; + } + +if (!config_send (msg, n)) { +rl_printf ("Failed to send \"%s NET KEY\"\n" , + opcode ==OP_NETKEY_ADD ?"ADD" :"DEL" ); +return ; + } + +if (opcode !=OP_NETKEY_DELETE ) { +if (node_net_key_add (node, net_idx)) +prov_db_node_keys (node,node_get_net_keys (node), +"netKeys" ); + }else { +if (node_net_key_delete (node, net_idx)) +prov_db_node_keys (node,node_get_net_keys (node), +"netKeys" ); + } + +} + +static void cmd_add_net_key (const char *args) +{ +cmd_net_key (args,OP_NETKEY_ADD ); +} + +static void cmd_del_net_key (const char *args) +{ +cmd_net_key (args,OP_NETKEY_DELETE ); +} + +static void cmd_app_key (const char *args,uint32_t opcode) +{ +uint16_t n; +uint8_t msg[32 ]; +uint16_t net_idx; +uint16_t app_idx; +uint8_t *key; +struct mesh_node *node; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + +if (read_input_parameters (args) !=1 ) { +rl_printf ("Bad arguments %s\n" , args); +return ; + } + + node =node_find_by_addr (target); +if (!node) { +rl_printf ("Node %4.4x\n not found" , target); +return ; + } + + n =mesh_opcode_set (opcode, msg); + + app_idx = parms[0 ]; + net_idx =keys_app_key_get_bound (app_idx); +if (net_idx ==NET_IDX_INVALID ) { +rl_printf ("App key with index %4.4x not found\n" , app_idx); +return ; + } + + msg[n++] = net_idx &0xf ; + msg[n++] = ((net_idx >>8 ) &0xf ) | + ((app_idx <<4 ) &0xf0 ); + msg[n++] = app_idx >>4 ; + +if (opcode !=OP_APPKEY_DELETE ) { + key =keys_app_key_get (app_idx, true); +if (!key) { +rl_printf ("App key %4.4x not found\n" , net_idx); +return ; + } + +memcpy (msg + n, key,16 ); + n +=16 ; + } + +if (!config_send (msg, n)) { +rl_printf ("Failed to send \"ADD %s KEY\"\n" , + opcode ==OP_APPKEY_ADD ?"ADD" :"DEL" ); +return ; + } + +if (opcode !=OP_APPKEY_DELETE ) { +if (node_app_key_add (node, app_idx)) +prov_db_node_keys (node,node_get_app_keys (node), +"appKeys" ); + }else { +if (node_app_key_delete (node, net_idx, app_idx)) +prov_db_node_keys (node,node_get_app_keys (node), +"appKeys" ); + } +} + +static void cmd_add_app_key (const char *args) +{ +cmd_app_key (args,OP_APPKEY_ADD ); +} + +static void cmd_del_app_key (const char *args) +{ +cmd_app_key (args,OP_APPKEY_DELETE ); +} + +static void cmd_bind (const char *args) +{ +uint16_t n; +uint8_t msg[32 ]; +int parm_cnt; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + parm_cnt =read_input_parameters (args); +if (parm_cnt !=3 && parm_cnt !=4 ) { +rl_printf ("Bad arguments %s\n" , args); +return ; + } + + n =mesh_opcode_set (OP_MODEL_APP_BIND , msg); + +put_le16 (target + parms[0 ], msg + n); + n +=2 ; +put_le16 (parms[1 ], msg + n); + n +=2 ; +if (parm_cnt ==4 ) { +put_le16 (parms[3 ], msg + n); +put_le16 (parms[2 ], msg + n +2 ); + n +=4 ; + }else { +put_le16 (parms[2 ], msg + n); + n +=2 ; + } + +if (!config_send (msg, n)) +rl_printf ("Failed to send \"MODEL APP BIND\"\n" ); +} + +static void cmd_set_ttl (const char *args) +{ +uint16_t n; +uint8_t msg[32 ]; +int parm_cnt; +uint8_t ttl; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + n =mesh_opcode_set (OP_CONFIG_DEFAULT_TTL_SET , msg); + + parm_cnt =read_input_parameters (args); +if (parm_cnt) { + ttl = parms[0 ] &TTL_MASK ; + }else + ttl =node_get_default_ttl (node_get_local_node ()); + + msg[n++] = ttl; + +if (!config_send (msg, n)) +rl_printf ("Failed to send \"SET_DEFAULT TTL\"\n" ); +} + +static void cmd_set_pub (const char *args) +{ +uint16_t n; +uint8_t msg[32 ]; +int parm_cnt; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + n =mesh_opcode_set (OP_CONFIG_MODEL_PUB_SET , msg); + + parm_cnt =read_input_parameters (args); +if (parm_cnt !=5 ) { +rl_printf ("Bad arguments: %s\n" , args); +return ; + } + +put_le16 (parms[0 ], msg + n); + n +=2 ; +/* Publish address */ +put_le16 (parms[1 ], msg + n); + n +=2 ; +/* App key index + credential (set to 0) */ +put_le16 (parms[2 ], msg + n); + n +=2 ; +/* TTL */ + msg[n++] =DEFAULT_TTL ; +/* Publish period step count and step resolution */ + msg[n++] = parms[3 ]; +/* Publish retransmit count & interval steps */ + msg[n++] = (1 <<5 ) +2 ; +/* Model Id */ +if (parms[4 ] >0xffff ) { +put_le16 (parms[4 ] >>16 , msg + n); +put_le16 (parms[4 ], msg + n +2 ); + n +=4 ; + }else { +put_le16 (parms[4 ], msg + n); + n +=2 ; + } + +if (!config_send (msg, n)) +rl_printf ("Failed to send \"SET MODEL PUBLICATION\"\n" ); +} + +static void cmd_default (uint32_t opcode) +{ +uint16_t n; +uint8_t msg[32 ]; + +if (IS_UNASSIGNED (target)) { +rl_printf ("Destination not set\n" ); +return ; + } + + n =mesh_opcode_set (opcode, msg); + +if (!config_send (msg, n)) +rl_printf ("Failed to send command (opcode 0x%x)\n" , opcode); +} + +static void cmd_get_ttl (const char *args) +{ +cmd_default (OP_CONFIG_DEFAULT_TTL_GET ); +} + +static void cmd_back (const char *args) +{ +cmd_menu_main (false); +} + +static void cmd_help (const char *args); + +static const struct menu_entry cfg_menu[] = { + {"target" ,"<unicast>" , cmd_set_node, +"Set target node to configure" }, + {"get-composition" ,"[<page_num>]" , cmd_get_composition, +"Get Composition Data" }, + {"add-netkey" ,"<net_idx>" , cmd_add_net_key, +"Add network key" }, + {"del-netkey" ,"<net_idx>" , cmd_del_net_key, +"Delete network key" }, + {"add-appkey" ,"<app_idx>" , cmd_add_app_key, +"Add application key" }, + {"del-appkey" ,"<app_idx>" , cmd_del_app_key, +"Delete application key" }, + {"bind" ,"<ele_idx> <app_idx> <mod_id> [cid]" , + cmd_bind,"Bind app key to a model" }, + {"set-ttl" ,"<ttl>" , cmd_set_ttl, +"Set default TTL" }, + {"get-ttl" ,NULL , cmd_get_ttl, +"Get default TTL" }, + {"set-pub" ,"<ele_addr> <pub_addr> <app_idx> " +"<period (step|res)> <model>" , + cmd_set_pub,"Set publication" }, + {"back" ,NULL , cmd_back, +"Back to main menu" }, + {"help" ,NULL , cmd_help, +"Config Commands" }, + {} +}; + +static void cmd_help (const char *args) +{ +rl_printf ("Client Configuration Menu\n" ); +print_cmd_menu (cfg_menu); +} + +void config_set_node (const char *args) +{ +cmd_set_node (args); +} + +void config_client_get_composition (uint32_t dst) +{ +uint32_t tmp = target; + + target = dst; +cmd_get_composition ("" ); + target = tmp; +} + +static struct mesh_model_ops client_cbs = { + client_msg_recvd, +NULL , +NULL , +NULL +}; + +bool config_client_init (void ) +{ +if (!node_local_model_register (PRIMARY_ELEMENT_IDX , +CONFIG_CLIENT_MODEL_ID , + &client_cbs,NULL )) +return false; + +add_cmd_menu ("configure" , cfg_menu); + +return true; +}mesh/config-server.c new file +165
-0@@ -0,0 +1,165 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdbool.h> +#include <inttypes.h> +#include <stdbool.h> +#include <sys/uio.h> +#include <wordexp.h> +#include <readline/readline.h> +#include <readline/history.h> +#include <glib.h> + +#include "src/shared/util.h" +#include "client/display.h" +#include "mesh-net.h" +#include "keys.h" +#include "net.h" +#include "node.h" +#include "prov-db.h" +#include "util.h" +#include "config-model.h" + +static bool server_msg_recvd (uint16_t src,uint8_t *data, +uint16_t len,void *user_data) +{ +uint32_t opcode; +uint8_t msg[32 ]; +struct mesh_node *node; +uint16_t primary; +uint32_t mod_id; +uint16_t ele_addr; +uint8_t ele_idx; +struct mesh_publication pub; + +int n; + +if (mesh_opcode_get (data, len, &opcode, &n)) { + len -= n; + data += n; + }else +return false; + + node =node_get_local_node (); + +if (!node) +return true; + +switch (opcode & ~OP_UNRELIABLE ) { +default : +return false; + +case OP_CONFIG_DEFAULT_TTL_SET : +if (len !=1 || data[0 ] >TTL_MASK || data[0 ] ==1 ) +return true; + +if (data[0 ] <=TTL_MASK ) { +node_set_default_ttl (node, data[0 ]); +prov_db_node_set_ttl (node, data[0 ]); + } + +/* Fall Through */ + +case OP_CONFIG_DEFAULT_TTL_GET : + n =mesh_opcode_set (OP_CONFIG_DEFAULT_TTL_STATUS , msg); + msg[n++] =node_get_default_ttl (node); +break ; + +case OP_CONFIG_MODEL_PUB_SET : +if (len !=11 && len !=13 ) +return true; + +rl_printf ("Set publication\n" ); + + ele_addr =get_le16 (data); + mod_id =get_le16 (data +9 ); +if (len ==14 ) + mod_id = (mod_id <<16 ) |get_le16 (data +11 ); +else + mod_id |=0xffff0000 ; + + pub.u.addr16 =get_le16 (data +2 ); + pub.app_idx =get_le16 (data +4 ); + pub.ttl = data[6 ]; + pub.period = data[7 ]; + n = (data[7 ] &0x3f ); +switch (data[7 ] >>6 ) { +case 0 : +rl_printf ("Period: %d ms\n" , n *100 ); +break ; +case 2 : + n *=10 ; +/* fall through */ +case 1 : +rl_printf ("Period: %d sec\n" , n); +break ; +case 3 : +rl_printf ("Period: %d min\n" , n *10 ); +break ; + } + + pub.retransmit = data[8 ]; +rl_printf ("Retransmit count: %d\n" , data[8 ] >>5 ); +rl_printf ("Retransmit Interval Steps: %d\n" , data[8 ] &0x1f ); + + ele_idx = ele_addr -node_get_primary (node); + +if (node_model_pub_set (node, ele_idx, mod_id, &pub)) { +prov_db_node_set_model_pub (node, ele_idx, mod_id, +node_model_pub_get (node, ele_idx, mod_id)); + } +break ; + } + + primary =node_get_primary (node); +if (src != primary) +net_access_layer_send (node_get_default_ttl (node), primary, + src,APP_IDX_DEV , msg, len); +return true; +} + + +static struct mesh_model_ops server_cbs = { + server_msg_recvd, +NULL , +NULL , +NULL +}; + +bool config_server_init (void ) +{ +if (!node_local_model_register (PRIMARY_ELEMENT_IDX , +CONFIG_SERVER_MODEL_ID , + &server_cbs,NULL )) +return false; + +return true; +}mesh/crypto.c new file +1168
-0@@ -0,0 +1,1168 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <fcntl.h> +#include <unistd.h> +#include <string.h> +#include <sys/socket.h> + +#include <linux/if_alg.h> + +#include <glib.h> + +#ifndef SOL_ALG +#define SOL_ALG 279 +#endif + +#ifndef ALG_SET_AEAD_AUTHSIZE +#define ALG_SET_AEAD_AUTHSIZE 5 +#endif + +#include "src/shared/util.h" +#include "mesh-net.h" +#include "crypto.h" + +static int alg_new (int fd,const void *keyval,socklen_t keylen, +size_t mic_size) +{ +if (setsockopt (fd,SOL_ALG ,ALG_SET_KEY , keyval, keylen) <0 ) { +g_printerr ("key" ); +return -1 ; + } + +if (mic_size && +setsockopt (fd,SOL_ALG , +ALG_SET_AEAD_AUTHSIZE ,NULL , mic_size) <0 ) { +g_printerr ("taglen" ); +return -1 ; + } + +/* FIXME: This should use accept4() with SOCK_CLOEXEC */ +return accept (fd,NULL ,0 ); +} + +static bool alg_encrypt (int fd,const void *inbuf,size_t inlen, +void *outbuf,size_t outlen) +{ +__u32 alg_op =ALG_OP_ENCRYPT ; +char cbuf[CMSG_SPACE (sizeof (alg_op))]; +struct cmsghdr *cmsg; +struct msghdr msg; +struct iovec iov; +ssize_t len; + +memset (cbuf,0 ,sizeof (cbuf)); +memset (&msg,0 ,sizeof (msg)); + + msg.msg_control = cbuf; + msg.msg_controllen =sizeof (cbuf); + + cmsg =CMSG_FIRSTHDR (&msg); + cmsg->cmsg_level =SOL_ALG ; + cmsg->cmsg_type =ALG_SET_OP ; + cmsg->cmsg_len =CMSG_LEN (sizeof (alg_op)); +memcpy (CMSG_DATA (cmsg), &alg_op,sizeof (alg_op)); + + iov.iov_base = (void *) inbuf; + iov.iov_len = inlen; + + msg.msg_iov = &iov; + msg.msg_iovlen =1 ; + + len =sendmsg (fd, &msg,0 ); +if (len <0 ) +return false; + + len =read (fd, outbuf, outlen); +if (len <0 ) +return false; + +return true; +} + +static int aes_ecb_setup (const uint8_t key[16 ]) +{ +struct sockaddr_alg salg; +int fd, nfd; + + fd =socket (PF_ALG ,SOCK_SEQPACKET |SOCK_CLOEXEC ,0 ); +if (fd <0 ) +return -1 ; + +memset (&salg,0 ,sizeof (salg)); + salg.salg_family =AF_ALG ; +strcpy ((char *) salg.salg_type,"skcipher" ); +strcpy ((char *) salg.salg_name,"ecb(aes)" ); + +if (bind (fd, (struct sockaddr *) &salg,sizeof (salg)) <0 ) { +close (fd); +return -1 ; + } + + nfd =alg_new (fd, key,16 ,0 ); + +close (fd); + +return nfd; +} + +static bool aes_ecb (int fd,const uint8_t plaintext[16 ],uint8_t encrypted[16 ]) +{ +return alg_encrypt (fd, plaintext,16 , encrypted,16 ); +} + +static void aes_ecb_destroy (int fd) +{ +close (fd); +} + +static bool aes_ecb_one (const uint8_t key[16 ], +const uint8_t plaintext[16 ],uint8_t encrypted[16 ]) +{ +bool result; +int fd; + + fd =aes_ecb_setup (key); +if (fd <0 ) +return false; + + result =aes_ecb (fd, plaintext, encrypted); + +aes_ecb_destroy (fd); + +return result; +} + +/* Maximum message length that can be passed to aes_cmac */ +#define CMAC_MSG_MAX (64 + 64 + 17) + +static int aes_cmac_setup (const uint8_t key[16 ]) +{ +struct sockaddr_alg salg; +int fd, nfd; + + fd =socket (PF_ALG ,SOCK_SEQPACKET |SOCK_CLOEXEC ,0 ); +if (fd <0 ) +return -1 ; + +memset (&salg,0 ,sizeof (salg)); + salg.salg_family =AF_ALG ; +strcpy ((char *) salg.salg_type,"hash" ); +strcpy ((char *) salg.salg_name,"cmac(aes)" ); + +if (bind (fd, (struct sockaddr *) &salg,sizeof (salg)) <0 ) { +close (fd); +return -1 ; + } + + nfd =alg_new (fd, key,16 ,0 ); + +close (fd); + +return nfd; +} + +static bool aes_cmac (int fd,const uint8_t *msg, +size_t msg_len,uint8_t res[16 ]) +{ +ssize_t len; + +if (msg_len >CMAC_MSG_MAX ) +return false; + + len =send (fd, msg, msg_len,0 ); +if (len <0 ) +return false; + + len =read (fd, res,16 ); +if (len <0 ) +return false; + +return true; +} + +static void aes_cmac_destroy (int fd) +{ +close (fd); +} + +static int aes_cmac_N_start (const uint8_t N [16 ]) +{ +int fd; + + fd =aes_cmac_setup (N ); +return fd; +} + +static bool aes_cmac_one (const uint8_t key[16 ],const void *msg, +size_t msg_len,uint8_t res[16 ]) +{ +bool result; +int fd; + + fd =aes_cmac_setup (key); +if (fd <0 ) +return false; + + result =aes_cmac (fd, msg, msg_len, res); + +aes_cmac_destroy (fd); + +return result; +} + +bool mesh_crypto_aes_cmac (const uint8_t key[16 ],const uint8_t *msg, +size_t msg_len,uint8_t res[16 ]) +{ +return aes_cmac_one (key, msg, msg_len, res); +} + +bool mesh_crypto_aes_ccm_encrypt (const uint8_t nonce[13 ],const uint8_t key[16 ], +const uint8_t *aad,uint16_t aad_len, +const uint8_t *msg,uint16_t msg_len, +uint8_t *out_msg,void *out_mic, +size_t mic_size) +{ +uint8_t pmsg[16 ], cmic[16 ], cmsg[16 ]; +uint8_t mic[16 ], Xn[16 ]; +uint16_t blk_cnt, last_blk; +bool result; +size_t i, j; +int fd; + +if (aad_len >=0xff00 ) { +g_printerr ("Unsupported AAD size" ); +return false; + } + + fd =aes_ecb_setup (key); +if (fd <0 ) +return false; + +/* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (0x0000 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmic); +if (!result) + goto done; + +/* X_0 = e(AppKey, 0x09 || nonce || length) */ +if (mic_size ==sizeof (uint64_t )) + pmsg[0 ] =0x19 | (aad_len ?0x40 :0x00 ); +else + pmsg[0 ] =0x09 | (aad_len ?0x40 :0x00 ); + +memcpy (pmsg +1 , nonce,13 ); +put_be16 (msg_len, pmsg +14 ); + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + +/* If AAD is being used to authenticate, include it here */ +if (aad_len) { +put_be16 (aad_len, pmsg); + +for (i =0 ; i <sizeof (uint16_t ); i++) + pmsg[i] = Xn[i] ^ pmsg[i]; + + j =0 ; + aad_len +=sizeof (uint16_t ); +while (aad_len >16 ) { +do { + pmsg[i] = Xn[i] ^ aad[j]; + i++, j++; + }while (i <16 ); + + aad_len -=16 ; + i =0 ; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + } + +for (i =0 ; i < aad_len; i++, j++) + pmsg[i] = Xn[i] ^ aad[j]; + +for (i = aad_len; i <16 ; i++) + pmsg[i] = Xn[i]; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + } + + last_blk = msg_len %16 ; + blk_cnt = (msg_len +15 ) /16 ; +if (!last_blk) + last_blk =16 ; + +for (j =0 ; j < blk_cnt; j++) { +if (j +1 == blk_cnt) { +/* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ +for (i =0 ; i < last_blk; i++) + pmsg[i] = Xn[i] ^ msg[(j *16 ) + i]; +for (i = last_blk; i <16 ; i++) + pmsg[i] = Xn[i] ^0x00 ; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + +/* MIC = C_mic ^ X_1 */ +for (i =0 ; i <sizeof (mic); i++) + mic[i] = cmic[i] ^ Xn[i]; + +/* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (j +1 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmsg); +if (!result) + goto done; + +if (out_msg) { +/* Encrypted = Payload[0-15] ^ C_1 */ +for (i =0 ; i < last_blk; i++) + out_msg[(j *16 ) + i] = + msg[(j *16 ) + i] ^ cmsg[i]; + + } + }else { +/* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ +for (i =0 ; i <16 ; i++) + pmsg[i] = Xn[i] ^ msg[(j *16 ) + i]; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + +/* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (j +1 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmsg); +if (!result) + goto done; + +if (out_msg) { +/* Encrypted = Payload[0-15] ^ C_N */ +for (i =0 ; i <16 ; i++) + out_msg[(j *16 ) + i] = + msg[(j *16 ) + i] ^ cmsg[i]; + } + + } + } + +if (out_msg) +memcpy (out_msg + msg_len, mic, mic_size); + +if (out_mic) { +switch (mic_size) { +case sizeof (uint32_t ): + *(uint32_t *)out_mic =get_be32 (mic); +break ; +case sizeof (uint64_t ): + *(uint64_t *)out_mic =get_be64 (mic); +break ; +default : +g_printerr ("Unsupported MIC size" ); + } + } + +done: +aes_ecb_destroy (fd); + +return result; +} + +bool mesh_crypto_aes_ccm_decrypt (const uint8_t nonce[13 ],const uint8_t key[16 ], +const uint8_t *aad,uint16_t aad_len, +const uint8_t *enc_msg,uint16_t enc_msg_len, +uint8_t *out_msg,void *out_mic, +size_t mic_size) +{ +uint8_t msg[16 ], pmsg[16 ], cmic[16 ], cmsg[16 ], Xn[16 ]; +uint8_t mic[16 ]; +uint16_t msg_len = enc_msg_len - mic_size; +uint16_t last_blk, blk_cnt; +bool result; +size_t i, j; +int fd; + +if (enc_msg_len <5 || aad_len >=0xff00 ) +return false; + + fd =aes_ecb_setup (key); +if (fd <0 ) +return false; + +/* C_mic = e(AppKey, 0x01 || nonce || 0x0000) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (0x0000 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmic); +if (!result) + goto done; + +/* X_0 = e(AppKey, 0x09 || nonce || length) */ +if (mic_size ==sizeof (uint64_t )) + pmsg[0 ] =0x19 | (aad_len ?0x40 :0x00 ); +else + pmsg[0 ] =0x09 | (aad_len ?0x40 :0x00 ); + +memcpy (pmsg +1 , nonce,13 ); +put_be16 (msg_len, pmsg +14 ); + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + +/* If AAD is being used to authenticate, include it here */ +if (aad_len) { +put_be16 (aad_len, pmsg); + +for (i =0 ; i <sizeof (uint16_t ); i++) + pmsg[i] = Xn[i] ^ pmsg[i]; + + j =0 ; + aad_len +=sizeof (uint16_t ); +while (aad_len >16 ) { +do { + pmsg[i] = Xn[i] ^ aad[j]; + i++, j++; + }while (i <16 ); + + aad_len -=16 ; + i =0 ; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + } + +for (i =0 ; i < aad_len; i++, j++) + pmsg[i] = Xn[i] ^ aad[j]; + +for (i = aad_len; i <16 ; i++) + pmsg[i] = Xn[i]; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + } + + last_blk = msg_len %16 ; + blk_cnt = (msg_len +15 ) /16 ; +if (!last_blk) + last_blk =16 ; + +for (j =0 ; j < blk_cnt; j++) { +if (j +1 == blk_cnt) { +/* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (j +1 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmsg); +if (!result) + goto done; + +/* Encrypted = Payload[0-15] ^ C_1 */ +for (i =0 ; i < last_blk; i++) + msg[i] = enc_msg[(j *16 ) + i] ^ cmsg[i]; + +if (out_msg) +memcpy (out_msg + (j *16 ), msg, last_blk); + +/* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ +for (i =0 ; i < last_blk; i++) + pmsg[i] = Xn[i] ^ msg[i]; +for (i = last_blk; i <16 ; i++) + pmsg[i] = Xn[i] ^0x00 ; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + +/* MIC = C_mic ^ X_1 */ +for (i =0 ; i <sizeof (mic); i++) + mic[i] = cmic[i] ^ Xn[i]; + }else { +/* C_1 = e(AppKey, 0x01 || nonce || 0x0001) */ + pmsg[0 ] =0x01 ; +memcpy (pmsg +1 , nonce,13 ); +put_be16 (j +1 , pmsg +14 ); + + result =aes_ecb (fd, pmsg, cmsg); +if (!result) + goto done; + +/* Encrypted = Payload[0-15] ^ C_1 */ +for (i =0 ; i <16 ; i++) + msg[i] = enc_msg[(j *16 ) + i] ^ cmsg[i]; + +if (out_msg) +memcpy (out_msg + (j *16 ), msg,16 ); + +/* X_1 = e(AppKey, X_0 ^ Payload[0-15]) */ +for (i =0 ; i <16 ; i++) + pmsg[i] = Xn[i] ^ msg[i]; + + result =aes_ecb (fd, pmsg, Xn); +if (!result) + goto done; + } + } + +switch (mic_size) { +case sizeof (uint32_t ): +if (out_mic) + *(uint32_t *)out_mic =get_be32 (mic); +else if (get_be32 (enc_msg + enc_msg_len - mic_size) != +get_be32 (mic)) + result = false; +break ; + +case sizeof (uint64_t ): +if (out_mic) + *(uint64_t *)out_mic =get_be64 (mic); +else if (get_be64 (enc_msg + enc_msg_len - mic_size) != +get_be64 (mic)) + result = false; +break ; + +default : +g_printerr ("Unsupported MIC size" ); + result = false; + } + +done: +aes_ecb_destroy (fd); + +return result; +} + +bool mesh_crypto_k1 (const uint8_t ikm[16 ],const uint8_t salt[16 ], +const void *info,size_t info_len,uint8_t okm[16 ]) +{ +uint8_t res[16 ]; + +if (!aes_cmac_one (salt, ikm,16 , res)) +return false; + +return aes_cmac_one (res, info, info_len, okm); +} + +bool mesh_crypto_k2 (const uint8_t n[16 ],const uint8_t *p,size_t p_len, +uint8_t net_id[1 ], +uint8_t enc_key[16 ], +uint8_t priv_key[16 ]) +{ +int fd; +uint8_t output[16 ]; +uint8_t t[16 ]; +uint8_t *stage; +bool success = false; + + stage =g_malloc (sizeof (output) + p_len +1 ); +if (stage ==NULL ) +return false; + +if (!mesh_crypto_s1 ("smk2" ,4 , stage)) + goto fail; + +if (!aes_cmac_one (stage, n,16 , t)) + goto fail; + + fd =aes_cmac_N_start (t); +if (fd <0 ) + goto fail; + +memcpy (stage, p, p_len); + stage[p_len] =1 ; + +if (!aes_cmac (fd, stage, p_len +1 , output)) + goto done; + + net_id[0 ] = output[15 ] &0x7f ; + +memcpy (stage, output,16 ); +memcpy (stage +16 , p, p_len); + stage[p_len +16 ] =2 ; + +if (!aes_cmac (fd, stage, p_len +16 +1 , output)) + goto done; + +memcpy (enc_key, output,16 ); + +memcpy (stage, output,16 ); +memcpy (stage +16 , p, p_len); + stage[p_len +16 ] =3 ; + +if (!aes_cmac (fd, stage, p_len +16 +1 , output)) + goto done; + +memcpy (priv_key, output,16 ); + success = true; + +done: +aes_cmac_destroy (fd); +fail: +g_free (stage); + +return success; +} + +static bool crypto_128 (const uint8_t n[16 ],const char *s,uint8_t out128[16 ]) +{ +uint8_t id128[] = {'i' ,'d' ,'1' ,'2' ,'8' ,0x01 }; +uint8_t salt[16 ]; + +if (!mesh_crypto_s1 (s,4 , salt)) +return false; + +return mesh_crypto_k1 (n, salt, id128,sizeof (id128), out128); +} + +bool mesh_crypto_nkik (const uint8_t n[16 ],uint8_t identity_key[16 ]) +{ +return crypto_128 (n,"nkik" , identity_key); +} + +static bool identity_calc (const uint8_t net_key[16 ],uint16_t addr, +bool check,uint8_t id[16 ]) +{ +uint8_t id_key[16 ]; +uint8_t tmp[16 ]; + +if (!mesh_crypto_nkik (net_key, id_key)) +return false; + +memset (tmp,0 ,sizeof (tmp)); +put_be16 (addr, tmp +14 ); + +if (check) { +memcpy (tmp +6 , id +8 ,8 ); + }else { +mesh_get_random_bytes (tmp +6 ,8 ); +memcpy (id +8 , tmp +6 ,8 ); + } + +if (!aes_ecb_one (id_key, tmp, tmp)) +return false; + +if (check) +return (memcmp (id, tmp +8 ,8 ) ==0 ); + +memcpy (id, tmp +8 ,8 ); +return true; +} + +bool mesh_crypto_identity (const uint8_t net_key[16 ],uint16_t addr, +uint8_t id[16 ]) +{ +return identity_calc (net_key, addr, false, id); +} + +bool mesh_crypto_identity_check (const uint8_t net_key[16 ],uint16_t addr, +uint8_t id[16 ]) +{ +return identity_calc (net_key, addr, true, id); +} + +bool mesh_crypto_nkbk (const uint8_t n[16 ],uint8_t beacon_key[16 ]) +{ +return crypto_128 (n,"nkbk" , beacon_key); +} + +bool mesh_crypto_k3 (const uint8_t n[16 ],uint8_t out64[8 ]) +{ +uint8_t tmp[16 ]; +uint8_t t[16 ]; +uint8_t id64[] = {'i' ,'d' ,'6' ,'4' ,0x01 }; + +if (!mesh_crypto_s1 ("smk3" ,4 , tmp)) +return false; + +if (!aes_cmac_one (tmp, n,16 , t)) +return false; + +if (!aes_cmac_one (t, id64,sizeof (id64), tmp)) +return false; + +memcpy (out64, tmp +8 ,8 ); + +return true; +} + +bool mesh_crypto_k4 (const uint8_t a[16 ],uint8_t out6[1 ]) +{ +uint8_t tmp[16 ]; +uint8_t t[16 ]; +uint8_t id6[] = {'i' ,'d' ,'6' ,0x01 }; + +if (!mesh_crypto_s1 ("smk4" ,4 , tmp)) +return false; + +if (!aes_cmac_one (tmp, a,16 , t)) +return false; + +if (!aes_cmac_one (t, id6,sizeof (id6), tmp)) +return false; + + out6[0 ] = tmp[15 ] &0x3f ; +return true; +} + +bool mesh_crypto_beacon_cmac (const uint8_t encryption_key[16 ], +const uint8_t network_id[8 ], +uint32_t iv_index,bool kr,bool iu, +uint64_t *cmac) +{ +uint8_t msg[13 ], tmp[16 ]; + +if (!cmac) +return false; + + msg[0 ] = kr ?0x01 :0x00 ; + msg[0 ] |= iu ?0x02 :0x00 ; +memcpy (msg +1 , network_id,8 ); +put_be32 (iv_index, msg +9 ); + +if (!aes_cmac_one (encryption_key, msg,13 , tmp)) +return false; + + *cmac =get_be64 (tmp); + +return true; +} + +bool mesh_crypto_network_nonce (bool ctl,uint8_t ttl,uint32_t seq, +uint16_t src,uint32_t iv_index, +uint8_t nonce[13 ]) +{ + nonce[0 ] =0 ; + nonce[1 ] = (ttl &TTL_MASK ) | (ctl ?CTL :0x00 ); + nonce[2 ] = (seq >>16 ) &0xff ; + nonce[3 ] = (seq >>8 ) &0xff ; + nonce[4 ] = seq &0xff ; + +/* SRC */ +put_be16 (src, nonce +5 ); + +put_be16 (0 , nonce +7 ); + +/* IV Index */ +put_be32 (iv_index, nonce +9 ); + +return true; +} + +bool mesh_crypto_network_encrypt (bool ctl,uint8_t ttl, +uint32_t seq,uint16_t src, +uint32_t iv_index, +const uint8_t net_key[16 ], +const uint8_t *enc_msg,uint8_t enc_msg_len, +uint8_t *out,void *net_mic) +{ +uint8_t nonce[13 ]; + +if (!mesh_crypto_network_nonce (ctl, ttl, seq, src, iv_index, nonce)) +return false; + +return mesh_crypto_aes_ccm_encrypt (nonce, net_key, +NULL ,0 , enc_msg, + enc_msg_len, out, + net_mic, + ctl ?sizeof (uint64_t ) :sizeof (uint32_t )); +} + +bool mesh_crypto_network_decrypt (bool ctl,uint8_t ttl, +uint32_t seq,uint16_t src, +uint32_t iv_index, +const uint8_t net_key[16 ], +const uint8_t *enc_msg,uint8_t enc_msg_len, +uint8_t *out,void *net_mic,size_t mic_size) +{ +uint8_t nonce[13 ]; + +if (!mesh_crypto_network_nonce (ctl, ttl, seq, src, iv_index, nonce)) +return false; + +return mesh_crypto_aes_ccm_decrypt (nonce, net_key,NULL ,0 , + enc_msg, enc_msg_len, out, + net_mic, mic_size); +} + +bool mesh_crypto_application_nonce (uint32_t seq,uint16_t src, +uint16_t dst,uint32_t iv_index, +bool aszmic,uint8_t nonce[13 ]) +{ + nonce[0 ] =0x01 ; + nonce[1 ] = aszmic ?0x80 :0x00 ; + nonce[2 ] = (seq &0x00ff0000 ) >>16 ; + nonce[3 ] = (seq &0x0000ff00 ) >>8 ; + nonce[4 ] = (seq &0x000000ff ); + nonce[5 ] = (src &0xff00 ) >>8 ; + nonce[6 ] = (src &0x00ff ); + nonce[7 ] = (dst &0xff00 ) >>8 ; + nonce[8 ] = (dst &0x00ff ); +put_be32 (iv_index, nonce +9 ); + +return true; +} + +bool mesh_crypto_device_nonce (uint32_t seq,uint16_t src, +uint16_t dst,uint32_t iv_index, +bool aszmic,uint8_t nonce[13 ]) +{ + nonce[0 ] =0x02 ; + nonce[1 ] = aszmic ?0x80 :0x00 ; + nonce[2 ] = (seq &0x00ff0000 ) >>16 ; + nonce[3 ] = (seq &0x0000ff00 ) >>8 ; + nonce[4 ] = (seq &0x000000ff ); + nonce[5 ] = (src &0xff00 ) >>8 ; + nonce[6 ] = (src &0x00ff ); + nonce[7 ] = (dst &0xff00 ) >>8 ; + nonce[8 ] = (dst &0x00ff ); +put_be32 (iv_index, nonce +9 ); + +return true; +} + +bool mesh_crypto_application_encrypt (uint8_t key_id,uint32_t seq,uint16_t src, +uint16_t dst,uint32_t iv_index, +const uint8_t app_key[16 ], +const uint8_t *aad,uint8_t aad_len, +const uint8_t *msg,uint8_t msg_len, +uint8_t *out,void *app_mic, +size_t mic_size) +{ +uint8_t nonce[13 ]; +bool aszmic = (mic_size ==sizeof (uint64_t )) ? true : false; + +if (!key_id && !mesh_crypto_device_nonce (seq, src, dst, + iv_index, aszmic, nonce)) +return false; + +if (key_id && !mesh_crypto_application_nonce (seq, src, dst, + iv_index, aszmic, nonce)) +return false; + +return mesh_crypto_aes_ccm_encrypt (nonce, app_key, aad, aad_len, + msg, msg_len, + out, app_mic, mic_size); +} + +bool mesh_crypto_application_decrypt (uint8_t key_id,uint32_t seq,uint16_t src, +uint16_t dst,uint32_t iv_index, +const uint8_t app_key[16 ], +const uint8_t *aad,uint8_t aad_len, +const uint8_t *enc_msg,uint8_t enc_msg_len, +uint8_t *out,void *app_mic,size_t mic_size) +{ +uint8_t nonce[13 ]; +bool aszmic = (mic_size ==sizeof (uint64_t )) ? true : false; + +if (!key_id && !mesh_crypto_device_nonce (seq, src, dst, + iv_index, aszmic, nonce)) +return false; + +if (key_id && !mesh_crypto_application_nonce (seq, src, dst, + iv_index, aszmic, nonce)) +return false; + +return mesh_crypto_aes_ccm_decrypt (nonce, app_key, + aad, aad_len, enc_msg, + enc_msg_len, out, + app_mic, mic_size); +} + +bool mesh_crypto_session_key (const uint8_t secret[32 ], +const uint8_t salt[16 ], +uint8_t session_key[16 ]) +{ +const uint8_t prsk[4 ] ="prsk" ; + +if (!aes_cmac_one (salt, secret,32 , session_key)) +return false; + +return aes_cmac_one (session_key, prsk,4 , session_key); +} + +bool mesh_crypto_nonce (const uint8_t secret[32 ], +const uint8_t salt[16 ], +uint8_t nonce[13 ]) +{ +const uint8_t prsn[4 ] ="prsn" ; +uint8_t tmp[16 ]; +bool result; + +if (!aes_cmac_one (salt, secret,32 , tmp)) +return false; + + result =aes_cmac_one (tmp, prsn,4 , tmp); + +if (result) +memcpy (nonce, tmp +3 ,13 ); + +return result; +} + +bool mesh_crypto_s1 (const void *info,size_t len,uint8_t salt[16 ]) +{ +const uint8_t zero[16 ] = {0 }; + +return aes_cmac_one (zero, info, len, salt); +} + +bool mesh_crypto_prov_prov_salt (const uint8_t conf_salt[16 ], +const uint8_t prov_rand[16 ], +const uint8_t dev_rand[16 ], +uint8_t prov_salt[16 ]) +{ +const uint8_t zero[16 ] = {0 }; +uint8_t tmp[16 *3 ]; + +memcpy (tmp, conf_salt,16 ); +memcpy (tmp +16 , prov_rand,16 ); +memcpy (tmp +32 , dev_rand,16 ); + +return aes_cmac_one (zero, tmp,sizeof (tmp), prov_salt); +} + +bool mesh_crypto_prov_conf_key (const uint8_t secret[32 ], +const uint8_t salt[16 ], +uint8_t conf_key[16 ]) +{ +const uint8_t prck[4 ] ="prck" ; + +if (!aes_cmac_one (salt, secret,32 , conf_key)) +return false; + +return aes_cmac_one (conf_key, prck,4 , conf_key); +} + +bool mesh_crypto_device_key (const uint8_t secret[32 ], +const uint8_t salt[16 ], +uint8_t device_key[16 ]) +{ +const uint8_t prdk[4 ] ="prdk" ; + +if (!aes_cmac_one (salt, secret,32 , device_key)) +return false; + +return aes_cmac_one (device_key, prdk,4 , device_key); +} + +bool mesh_crypto_virtual_addr (const uint8_t virtual_label[16 ], +uint16_t *addr) +{ +uint8_t tmp[16 ]; + +if (!mesh_crypto_s1 ("vtad" ,4 , tmp)) +return false; + +if (!addr || !aes_cmac_one (tmp, virtual_label,16 , tmp)) +return false; + + *addr = (get_be16 (tmp +14 ) &0x3fff ) |0x8000 ; + +return true; +} + +bool mesh_crypto_packet_encode (uint8_t *packet,uint8_t packet_len, +const uint8_t network_key[16 ], +uint32_t iv_index, +const uint8_t privacy_key[16 ]) +{ +uint8_t network_nonce[13 ] = {0x00 ,0x00 }; +uint8_t privacy_counter[16 ] = {0x00 ,0x00 ,0x00 ,0x00 ,0x00 , }; +uint8_t tmp[16 ]; +int i; + +/* Detect Proxy packet by CTL == true && DST == 0x0000 */ +if ((packet[1 ] &CTL ) &&get_be16 (packet +7 ) ==0 ) + network_nonce[0 ] =0x03 ; +else +/* CTL + TTL */ + network_nonce[1 ] = packet[1 ]; + +/* Seq Num */ + network_nonce[2 ] = packet[2 ]; + network_nonce[3 ] = packet[3 ]; + network_nonce[4 ] = packet[4 ]; + +/* SRC */ + network_nonce[5 ] = packet[5 ]; + network_nonce[6 ] = packet[6 ]; + +/* DST not available */ + network_nonce[7 ] =0 ; + network_nonce[8 ] =0 ; + +/* IV Index */ +put_be32 (iv_index, network_nonce +9 ); + +/* Check for Long net-MIC */ +if (packet[1 ] &CTL ) { +if (!mesh_crypto_aes_ccm_encrypt (network_nonce, network_key, +NULL ,0 , + packet +7 , packet_len -7 -8 , + packet +7 ,NULL ,sizeof (uint64_t ))) +return false; + }else { +if (!mesh_crypto_aes_ccm_encrypt (network_nonce, network_key, +NULL ,0 , + packet +7 , packet_len -7 -4 , + packet +7 ,NULL ,sizeof (uint32_t ))) +return false; + } + +put_be32 (iv_index, privacy_counter +5 ); +memcpy (privacy_counter +9 , packet +7 ,7 ); + +if (!aes_ecb_one (privacy_key, privacy_counter, tmp)) +return false; + +for (i =0 ; i <6 ; i++) + packet[1 + i] ^= tmp[i]; + +return true; +} + +bool mesh_crypto_packet_decode (const uint8_t *packet,uint8_t packet_len, +bool proxy,uint8_t *out,uint32_t iv_index, +const uint8_t network_key[16 ], +const uint8_t privacy_key[16 ]) +{ +uint8_t privacy_counter[16 ] = {0x00 ,0x00 ,0x00 ,0x00 ,0x00 , }; +uint8_t network_nonce[13 ] = {0x00 ,0x00 , }; +uint8_t tmp[16 ]; +uint16_t src; +int i; + +if (packet_len <14 ) +return false; + +put_be32 (iv_index, privacy_counter +5 ); +memcpy (privacy_counter +9 , packet +7 ,7 ); + +if (!aes_ecb_one (privacy_key, privacy_counter, tmp)) +return false; + +memcpy (out, packet, packet_len); +for (i =0 ; i <6 ; i++) + out[1 + i] ^= tmp[i]; + + src =get_be16 (out +5 ); + +/* Pre-check SRC address for illegal values */ +if (!src || src >=0x8000 ) +return false; + +/* Detect Proxy packet by CTL == true && proxy == true */ +if ((out[1 ] &CTL ) && proxy) + network_nonce[0 ] =0x03 ; +else +/* CTL + TTL */ + network_nonce[1 ] = out[1 ]; + +/* Seq Num */ + network_nonce[2 ] = out[2 ]; + network_nonce[3 ] = out[3 ]; + network_nonce[4 ] = out[4 ]; + +/* SRC */ + network_nonce[5 ] = out[5 ]; + network_nonce[6 ] = out[6 ]; + +/* DST not available */ + network_nonce[7 ] =0 ; + network_nonce[8 ] =0 ; + +/* IV Index */ +put_be32 (iv_index, network_nonce +9 ); + +/* Check for Long MIC */ +if (out[1 ] &CTL ) { +uint64_t mic; + +if (!mesh_crypto_aes_ccm_decrypt (network_nonce, network_key, +NULL ,0 , packet +7 , packet_len -7 , + out +7 , &mic,sizeof (mic))) +return false; + + mic ^=get_be64 (out + packet_len -8 ); +put_be64 (mic, out + packet_len -8 ); + +if (mic) +return false; + }else { +uint32_t mic; + +if (!mesh_crypto_aes_ccm_decrypt (network_nonce, network_key, +NULL ,0 , packet +7 , packet_len -7 , + out +7 , &mic,sizeof (mic))) +return false; + + mic ^=get_be32 (out + packet_len -4 ); +put_be32 (mic, out + packet_len -4 ); + +if (mic) +return false; + } + +return true; +} + +bool mesh_get_random_bytes (void *buf,size_t num_bytes) +{ +ssize_t len; +int fd; + + fd =open ("/dev/urandom" ,O_RDONLY ); +if (fd <0 ) +return false; + + len =read (fd, buf, num_bytes); + +close (fd); + +if (len <0 ) +return false; + +return true; +}mesh/gatt.c new file +609
-0@@ -0,0 +1,609 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdbool.h> +#include <sys/uio.h> +#include <wordexp.h> + +#include <readline/readline.h> +#include <readline/history.h> +#include <glib.h> + +#include "src/shared/io.h" +#include "gdbus/gdbus.h" +#include "lib/bluetooth.h" +#include "lib/uuid.h" +#include "client/display.h" +#include "node.h" +#include "util.h" +#include "gatt.h" +#include "prov.h" +#include "net.h" + +#define MESH_PROV_DATA_OUT_UUID_STR "00002adc-0000-1000-8000-00805f9b34fb" +#define MESH_PROXY_DATA_OUT_UUID_STR "00002ade-0000-1000-8000-00805f9b34fb" + +static struct io *write_io; +static uint16_t write_mtu; + +static struct io *notify_io; +static uint16_t notify_mtu; + +struct write_data { +GDBusProxy *proxy; +void *user_data; +struct iovec iov; +GDBusReturnFunction cb; +uint8_t *gatt_data; +uint8_t gatt_len; +}; + +struct notify_data { +GDBusProxy *proxy; +bool enable; +GDBusReturnFunction cb; +void *user_data; +}; + +bool mesh_gatt_is_child (GDBusProxy *proxy,GDBusProxy *parent, +const char *name) +{ +DBusMessageIter iter; +const char *parent_path; + +if (!parent) +return FALSE; + +if (g_dbus_proxy_get_property (proxy, name, &iter) == FALSE) +return FALSE; + +dbus_message_iter_get_basic (&iter, &parent_path); + +if (!strcmp (parent_path,g_dbus_proxy_get_path (parent))) +return TRUE; +else +return FALSE; +} + +/* Refactor this once actual MTU is available */ +#define GATT_MTU 23 + +static void write_data_free (void *user_data) +{ +struct write_data *data = user_data; + +g_free (data->gatt_data); +free (data); +} + +static void write_setup (DBusMessageIter *iter,void *user_data) +{ +struct write_data *data = user_data; +struct iovec *iov = &data->iov; +DBusMessageIter array, dict; + +dbus_message_iter_open_container (iter,DBUS_TYPE_ARRAY ,"y" , &array); +dbus_message_iter_append_fixed_array (&array,DBUS_TYPE_BYTE , + &iov->iov_base, iov->iov_len); +dbus_message_iter_close_container (iter, &array); + +dbus_message_iter_open_container (iter,DBUS_TYPE_ARRAY , + DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING + DBUS_TYPE_STRING_AS_STRING + DBUS_TYPE_VARIANT_AS_STRING +DBUS_DICT_ENTRY_END_CHAR_AS_STRING , + &dict); + +dbus_message_iter_close_container (iter, &dict); +} + +uint16_t mesh_gatt_sar (uint8_t **pkt,uint16_t size) +{ +const uint8_t *data = *pkt; +uint8_t gatt_hdr = *data++; +uint8_t type = gatt_hdr &GATT_TYPE_MASK ; +static uint8_t gatt_size; +static uint8_t gatt_pkt[67 ]; + +print_byte_array ("GATT-RX:\t" , *pkt, size); +if (size <1 ) { + gatt_pkt[0 ] =GATT_TYPE_INVALID ; +/* TODO: Disconnect GATT per last paragraph sec 6.6 */ +return 0 ; + } + + size--; + +switch (gatt_hdr &GATT_SAR_MASK ) { +case GATT_SAR_FIRST : + gatt_size =1 ; + gatt_pkt[0 ] = type; +/* TODO: Start Proxy Timeout */ +/* fall through */ + +case GATT_SAR_CONTINUE : +if (gatt_pkt[0 ] != type || + gatt_size + size >MAX_GATT_SIZE ) { + +/* Invalidate packet and return failure */ + gatt_pkt[0 ] =GATT_TYPE_INVALID ; +/* TODO: Disconnect GATT per last paragraph sec 6.6 */ +return 0 ; + } + +memcpy (gatt_pkt + gatt_size, data, size); + gatt_size += size; + +/* We are good to this point, but incomplete */ +return 0 ; + +default : +case GATT_SAR_COMPLETE : + gatt_size =1 ; + gatt_pkt[0 ] = type; + +/* fall through */ + +case GATT_SAR_LAST : +if (gatt_pkt[0 ] != type || + gatt_size + size >MAX_GATT_SIZE ) { + +/* Invalidate packet and return failure */ + gatt_pkt[0 ] =GATT_TYPE_INVALID ; +/* Disconnect GATT per last paragraph sec 6.6 */ +return 0 ; + } + +memcpy (gatt_pkt + gatt_size, data, size); + gatt_size += size; + *pkt = gatt_pkt; +return gatt_size; + } +} + +static bool pipe_write (struct io *io,void *user_data) +{ +struct write_data *data = user_data; +struct iovec iov[2 ]; +uint8_t sar; +uint8_t max_len = write_mtu -4 ; + +if (data ==NULL ) +return true; + +print_byte_array ("GATT-TX:\t" , data->gatt_data, data->gatt_len); + + sar = data->gatt_data[0 ]; + + data->iov.iov_base = data->gatt_data +1 ; + data->iov.iov_len--; + + iov[0 ].iov_base = &sar; + iov[0 ].iov_len =sizeof (sar); + +while (1 ) { +int err; + + iov[1 ] = data->iov; + + err =io_send (io, iov,2 ); +if (err <0 ) { +rl_printf ("Failed to write: %s\n" ,strerror (-err)); +write_data_free (data); +return false; + } + +switch (sar &GATT_SAR_MASK ) { +case GATT_SAR_FIRST : +case GATT_SAR_CONTINUE : + data->gatt_len -= max_len; + data->iov.iov_base = data->iov.iov_base + max_len; + + sar &=GATT_TYPE_MASK ; +if (max_len < data->gatt_len) { + data->iov.iov_len = max_len; + sar |=GATT_SAR_CONTINUE ; + }else { + data->iov.iov_len = data->gatt_len; + sar |=GATT_SAR_LAST ; + } + +break ; + +default : +if (data->cb) + data->cb (NULL , data->user_data); +write_data_free (data); +return true; + } + } +} + +static void write_reply (DBusMessage *message,void *user_data) +{ +struct write_data *data = user_data; +struct write_data *tmp; +uint8_t *dptr = data->gatt_data; +uint8_t max_len =GATT_MTU -3 ; +uint8_t max_seg =GATT_MTU -4 ; +DBusError error; + +dbus_error_init (&error); + +if (dbus_set_error_from_message (&error, message) == TRUE) { +rl_printf ("Failed to write: %s\n" , error.name); +dbus_error_free (&error); +return ; + } + +if (data ==NULL ) +return ; + +switch (data->gatt_data[0 ] &GATT_SAR_MASK ) { +case GATT_SAR_FIRST : +case GATT_SAR_CONTINUE : + tmp = g_new0(struct write_data, 1); +if (!data) +return ; + + *tmp = *data; + tmp->gatt_data =g_malloc (data->gatt_len); + +if (!tmp->gatt_data) { +g_free (tmp); +return ; + } + + tmp->gatt_data[0 ] = dptr[0 ]; + data = tmp; +memcpy (data->gatt_data +1 , dptr + max_len, + data->gatt_len - max_seg); + data->gatt_len -= max_seg; + data->gatt_data[0 ] &=GATT_TYPE_MASK ; + data->iov.iov_base = data->gatt_data; +if (max_len < data->gatt_len) { + data->iov.iov_len = max_len; + data->gatt_data[0 ] |=GATT_SAR_CONTINUE ; + }else { + data->iov.iov_len = data->gatt_len; + data->gatt_data[0 ] |=GATT_SAR_LAST ; + } + +break ; + +default : +if (data->cb) + data->cb (message, data->user_data); +return ; + } + +if (g_dbus_proxy_method_call (data->proxy,"WriteValue" , write_setup, + write_reply, data, write_data_free) == FALSE) { +rl_printf ("Failed to write\n" ); +write_data_free (data); +return ; + } + +} + +static void write_io_destroy (void ) +{ +io_destroy (write_io); + write_io =NULL ; + write_mtu =0 ; +} + +static void notify_io_destroy (void ) +{ +io_destroy (notify_io); + notify_io =NULL ; + notify_mtu =0 ; +} + +static bool pipe_hup (struct io *io,void *user_data) +{ +rl_printf ("%s closed\n" , io == notify_io ?"Notify" :"Write" ); + +if (io == notify_io) +notify_io_destroy (); +else +write_io_destroy (); + +return false; +} + +static struct io *pipe_io_new (int fd) +{ +struct io *io; + + io =io_new (fd); + +io_set_close_on_destroy (io, true); + +io_set_disconnect_handler (io, pipe_hup,NULL ,NULL ); + +return io; +} + +static void acquire_write_reply (DBusMessage *message,void *user_data) +{ +struct write_data *data = user_data; +DBusError error; +int fd; + +dbus_error_init (&error); + +if (dbus_set_error_from_message (&error, message) == TRUE) { +dbus_error_free (&error); +if (g_dbus_proxy_method_call (data->proxy,"WriteValue" , + write_setup, write_reply, data, + write_data_free) == FALSE) { +rl_printf ("Failed to write\n" ); +write_data_free (data); + } +return ; + } + +if ((dbus_message_get_args (message,NULL ,DBUS_TYPE_UNIX_FD , &fd, +DBUS_TYPE_UINT16 , &write_mtu, +DBUS_TYPE_INVALID ) == false)) { +rl_printf ("Invalid AcquireWrite response\n" ); +return ; + } + +rl_printf ("AcquireWrite success: fd %d MTU %u\n" , fd, write_mtu); + + write_io =pipe_io_new (fd); + +pipe_write (write_io, data); +} + +bool mesh_gatt_write (GDBusProxy *proxy,uint8_t *buf,uint16_t len, +GDBusReturnFunction cb,void *user_data) +{ +struct write_data *data; +DBusMessageIter iter; +uint8_t max_len; + +if (!buf || !len) +return false; + +if (len >69 ) +return false; + + data = g_new0(struct write_data, 1); +if (!data) +return false; + + max_len = write_mtu ? write_mtu -3 :GATT_MTU -3 ; + +/* TODO: should keep in queue in case we need to cancel write? */ + + data->gatt_len = len; + data->gatt_data =g_memdup (buf, len); + data->gatt_data[0 ] &=GATT_TYPE_MASK ; +if (max_len < len) { + len = max_len; + data->gatt_data[0 ] |=GATT_SAR_FIRST ; + } + data->iov.iov_base = data->gatt_data; + data->iov.iov_len = len; + data->proxy = proxy; + data->user_data = user_data; + data->cb = cb; + +if (write_io) +return pipe_write (write_io, data); + +if (g_dbus_proxy_get_property (proxy,"WriteAcquired" , &iter)) { +if (g_dbus_proxy_method_call (proxy,"AcquireWrite" ,NULL , + acquire_write_reply, data,NULL ) == FALSE) { +rl_printf ("Failed to AcquireWrite\n" ); +write_data_free (data); +return false; + } + }else { +if (g_dbus_proxy_method_call (data->proxy,"WriteValue" , + write_setup, write_reply, data, + write_data_free) == FALSE) { +rl_printf ("Failed to write\n" ); +write_data_free (data); +return false; + } +print_byte_array ("GATT-TX: " , buf, len); +rl_printf ("Attempting to write %s\n" , +g_dbus_proxy_get_path (proxy)); + } + +return true; +} + +static void notify_reply (DBusMessage *message,void *user_data) +{ +struct notify_data *data = user_data; +DBusError error; + +dbus_error_init (&error); + +if (dbus_set_error_from_message (&error, message) == TRUE) { +rl_printf ("Failed to %s notify: %s\n" , + data->enable ?"start" :"stop" , error.name); +dbus_error_free (&error); + goto done; + } + +rl_printf ("Notify %s\n" , data->enable ?"started" :"stopped" ); + +done: +if (data->cb) + data->cb (message, data->user_data); + +g_free (data); +} + +static bool pipe_read (struct io *io,bool prov,void *user_data) +{ +struct mesh_node *node = user_data; +uint8_t buf[512 ]; +uint8_t *res; +int fd =io_get_fd (io); +ssize_t len; + +if (io != notify_io) +return true; + +while ((len =read (fd, buf,sizeof (buf)))) { +if (len <=0 ) +break ; + + res = buf; +mesh_gatt_sar (&res, len); + +if (prov) +prov_data_ready (node, res, len); +else +net_data_ready (res, len); + } + +return true; +} + +static bool pipe_read_prov (struct io *io,void *user_data) +{ +return pipe_read (io, true, user_data); +} + +static bool pipe_read_proxy (struct io *io,void *user_data) +{ +return pipe_read (io, false, user_data); +} + +static void acquire_notify_reply (DBusMessage *message,void *user_data) +{ +struct notify_data *data = user_data; +DBusMessageIter iter; +DBusError error; +int fd; +const char *uuid; + +dbus_error_init (&error); + +if (dbus_set_error_from_message (&error, message) == TRUE) { +dbus_error_free (&error); +if (g_dbus_proxy_method_call (data->proxy,"StartNotify" ,NULL , + notify_reply, data,NULL ) == FALSE) { +rl_printf ("Failed to StartNotify\n" ); +g_free (data); + } +return ; + } + +if (notify_io) { +io_destroy (notify_io); + notify_io =NULL ; + } + + notify_mtu =0 ; + +if ((dbus_message_get_args (message,NULL ,DBUS_TYPE_UNIX_FD , &fd, +DBUS_TYPE_UINT16 , ¬ify_mtu, +DBUS_TYPE_INVALID ) == false)) { +if (g_dbus_proxy_method_call (data->proxy,"StartNotify" ,NULL , + notify_reply, data,NULL ) == FALSE) { +rl_printf ("Failed to StartNotify\n" ); +g_free (data); + } +return ; + } + +rl_printf ("AcquireNotify success: fd %d MTU %u\n" , fd, notify_mtu); + +if (g_dbus_proxy_get_property (data->proxy,"UUID" , &iter) == FALSE) + goto done; + + notify_io =pipe_io_new (fd); + +dbus_message_iter_get_basic (&iter, &uuid); + +if (!bt_uuid_strcmp (uuid,MESH_PROV_DATA_OUT_UUID_STR )) +io_set_read_handler (notify_io, pipe_read_prov, data->user_data, +NULL ); +else if (!bt_uuid_strcmp (uuid,MESH_PROXY_DATA_OUT_UUID_STR )) +io_set_read_handler (notify_io, pipe_read_proxy, data->user_data, +NULL ); + +done: +if (data->cb) + data->cb (message, data->user_data); + +g_free (data); +} + +bool mesh_gatt_notify (GDBusProxy *proxy,bool enable,GDBusReturnFunction cb, +void *user_data) +{ +struct notify_data *data; +DBusMessageIter iter; +const char *method; + + data = g_new0(struct notify_data, 1); + data->proxy = proxy; + data->enable = enable; + data->cb = cb; + data->user_data = user_data; + +if (enable == TRUE) { +if (g_dbus_proxy_get_property (proxy,"NotifyAcquired" , &iter)) { + method ="AcquireNotify" ; + cb = acquire_notify_reply; + }else { + method ="StartNotify" ; + cb = notify_reply; + } + }else { +if (notify_io) { +notify_io_destroy (); +if (cb) +cb (NULL , user_data); +return true; + }else { + method ="StopNotify" ; + cb = notify_reply; + } + } + +if (g_dbus_proxy_method_call (proxy, method,NULL , cb, + data,NULL ) == FALSE) { +rl_printf ("Failed to %s\n" , method); +return false; + } +return true; +}mesh/main.c new file +2269
-0@@ -0,0 +1,2269 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2017 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +* You should have received a copy of the GNU Lesser General Public +* License along with this library; if not, write to the Free Software +* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +* +*/ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdio.h> +#include <errno.h> +#include <unistd.h> +#include <stdlib.h> +#include <stdbool.h> +#include <signal.h> +#include <sys/signalfd.h> +#include <wordexp.h> + +#include <inttypes.h> +#include <ctype.h> +#include <sys/file.h> +#include <sys/ioctl.h> +#include <sys/stat.h> +#include "bluetooth/bluetooth.h" + +#include <readline/readline.h> +#include <readline/history.h> +#include <glib.h> + +#include "lib/bluetooth.h" +#include "lib/uuid.h" +#include "src/shared/util.h" +#include "gdbus/gdbus.h" +#include "monitor/uuid.h" +#include "client/display.h" +#include "mesh-net.h" +#include "gatt.h" +#include "crypto.h" +#include "node.h" +#include "net.h" +#include "keys.h" +#include "prov.h" +#include "util.h" +#include "agent.h" +#include "prov-db.h" +#include "config-model.h" +#include "onoff-model.h" + +/* String display constants */ +#define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF +#define COLORED_CHG COLOR_YELLOW "CHG" COLOR_OFF +#define COLORED_DEL COLOR_RED "DEL" COLOR_OFF + +#define PROMPT_ON COLOR_BLUE "[meshctl]" COLOR_OFF "# " +#define PROMPT_OFF "Waiting to connect to bluetoothd..." + +#define MESH_PROV_DATA_IN_UUID_STR "00002adb-0000-1000-8000-00805f9b34fb" +#define MESH_PROV_DATA_OUT_UUID_STR "00002adc-0000-1000-8000-00805f9b34fb" +#define MESH_PROXY_DATA_IN_UUID_STR "00002add-0000-1000-8000-00805f9b34fb" +#define MESH_PROXY_DATA_OUT_UUID_STR "00002ade-0000-1000-8000-00805f9b34fb" + +static GMainLoop *main_loop; +static DBusConnection *dbus_conn; + +struct adapter { +GDBusProxy *proxy; +GList *mesh_devices; +}; + +struct mesh_device { +GDBusProxy *proxy; +uint8_t dev_uuid[16 ]; +gboolean hide; +}; + +GList *service_list; +GList *char_list; + +static GList *ctrl_list; +static struct adapter *default_ctrl; + +static char *mesh_prov_db_filename; +static char *mesh_local_config_filename; + +static bool discovering = false; +static bool discover_mesh; +static uint16_t prov_net_key_index =NET_IDX_PRIMARY ; + +static guint input =0 ; + +#define CONN_TYPE_NETWORK 0x00 +#define CONN_TYPE_IDENTITY 0x01 +#define CONN_TYPE_PROVISION 0x02 +#define CONN_TYPE_INVALID 0xff + +#define NET_IDX_INVALID 0xffff +Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
2.9.5
Signature · 1 line
Luiz Augusto von Dentz
Part of the diff is not shown here. View the raw message.
RE: [PATCH 4/5] mesh: Baseline Mesh implementation
7,015 lines not shown · raw message
Part of this message is not shown here. View the raw message.