1 /* main.c - Application main entry point */
2 
3 /*
4  * Copyright (c) 2017 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #include <zephyr/sys/printk.h>
10 
11 #include <zephyr/bluetooth/bluetooth.h>
12 #include <zephyr/bluetooth/mesh.h>
13 
14 #define MAX_FAULT 24
15 
16 static bool has_reg_fault = true;
17 
fault_get_cur(struct bt_mesh_model * model,uint8_t * test_id,uint16_t * company_id,uint8_t * faults,uint8_t * fault_count)18 static int fault_get_cur(struct bt_mesh_model *model, uint8_t *test_id,
19 			 uint16_t *company_id, uint8_t *faults, uint8_t *fault_count)
20 {
21 	uint8_t reg_faults[MAX_FAULT] = { [0 ... (MAX_FAULT - 1)] = 0xff };
22 
23 	printk("fault_get_cur() has_reg_fault %u\n", has_reg_fault);
24 
25 	*test_id = 0x00;
26 	*company_id = BT_COMP_ID_LF;
27 	memcpy(faults, reg_faults, sizeof(reg_faults));
28 	*fault_count = sizeof(reg_faults);
29 
30 	return 0;
31 }
32 
fault_get_reg(struct bt_mesh_model * model,uint16_t company_id,uint8_t * test_id,uint8_t * faults,uint8_t * fault_count)33 static int fault_get_reg(struct bt_mesh_model *model, uint16_t company_id,
34 			 uint8_t *test_id, uint8_t *faults, uint8_t *fault_count)
35 {
36 	if (company_id != BT_COMP_ID_LF) {
37 		return -EINVAL;
38 	}
39 
40 	printk("fault_get_reg() has_reg_fault %u\n", has_reg_fault);
41 
42 	*test_id = 0x00;
43 
44 	if (has_reg_fault) {
45 		uint8_t reg_faults[MAX_FAULT] = { [0 ... (MAX_FAULT - 1)] = 0xff };
46 
47 		memcpy(faults, reg_faults, sizeof(reg_faults));
48 		*fault_count = sizeof(reg_faults);
49 	} else {
50 		*fault_count = 0U;
51 	}
52 
53 	return 0;
54 }
55 
fault_clear(struct bt_mesh_model * model,uint16_t company_id)56 static int fault_clear(struct bt_mesh_model *model, uint16_t company_id)
57 {
58 	if (company_id != BT_COMP_ID_LF) {
59 		return -EINVAL;
60 	}
61 
62 	has_reg_fault = false;
63 
64 	return 0;
65 }
66 
fault_test(struct bt_mesh_model * model,uint8_t test_id,uint16_t company_id)67 static int fault_test(struct bt_mesh_model *model, uint8_t test_id,
68 		      uint16_t company_id)
69 {
70 	if (company_id != BT_COMP_ID_LF) {
71 		return -EINVAL;
72 	}
73 
74 	has_reg_fault = true;
75 	bt_mesh_health_srv_fault_update(bt_mesh_model_elem(model));
76 
77 	return 0;
78 }
79 
80 static const struct bt_mesh_health_srv_cb health_srv_cb = {
81 	.fault_get_cur = fault_get_cur,
82 	.fault_get_reg = fault_get_reg,
83 	.fault_clear = fault_clear,
84 	.fault_test = fault_test,
85 };
86 
87 static struct bt_mesh_health_srv health_srv = {
88 	.cb = &health_srv_cb,
89 };
90 
91 BT_MESH_HEALTH_PUB_DEFINE(health_pub, MAX_FAULT);
92 
93 static struct bt_mesh_model root_models[] = {
94 	BT_MESH_MODEL_CFG_SRV,
95 	BT_MESH_MODEL_HEALTH_SRV(&health_srv, &health_pub),
96 };
97 
vnd_publish(struct bt_mesh_model * mod)98 static int vnd_publish(struct bt_mesh_model *mod)
99 {
100 	printk("Vendor publish\n");
101 	return 0;
102 }
103 
104 BT_MESH_MODEL_PUB_DEFINE(vnd_pub, vnd_publish, 4);
105 
106 BT_MESH_MODEL_PUB_DEFINE(vnd_pub2, NULL, 4);
107 
108 static const struct bt_mesh_model_op vnd_ops[] = {
109 	BT_MESH_MODEL_OP_END,
110 };
111 
112 static struct bt_mesh_model vnd_models[] = {
113 	BT_MESH_MODEL_VND(BT_COMP_ID_LF, 0x1234, vnd_ops, &vnd_pub, NULL),
114 	BT_MESH_MODEL_VND(BT_COMP_ID_LF, 0x4321, vnd_ops, &vnd_pub2, NULL),
115 };
116 
117 static struct bt_mesh_elem elements[] = {
118 	BT_MESH_ELEM(0, root_models, vnd_models),
119 };
120 
121 static const struct bt_mesh_comp comp = {
122 	.cid = BT_COMP_ID_LF,
123 	.elem = elements,
124 	.elem_count = ARRAY_SIZE(elements),
125 };
126 
127 #if 0
128 static int output_number(bt_mesh_output_action_t action, uint32_t number)
129 {
130 	printk("OOB Number: %u\n", number);
131 
132 	board_output_number(action, number);
133 
134 	return 0;
135 }
136 #endif
137 
prov_complete(uint16_t net_idx,uint16_t addr)138 static void prov_complete(uint16_t net_idx, uint16_t addr)
139 {
140 	if (IS_ENABLED(CONFIG_BT_MESH_IV_UPDATE_TEST)) {
141 		bt_mesh_iv_update_test(true);
142 	}
143 }
144 
prov_reset(void)145 static void prov_reset(void)
146 {
147 	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
148 }
149 
150 static const uint8_t dev_uuid[16] = { 0xdd, 0xdd };
151 
152 static const struct bt_mesh_prov prov = {
153 	.uuid = dev_uuid,
154 #if 0
155 	.output_size = 4,
156 	.output_actions = BT_MESH_DISPLAY_NUMBER,
157 	.output_number = output_number,
158 #endif
159 	.complete = prov_complete,
160 	.reset = prov_reset,
161 };
162 
bt_ready(int err)163 static void bt_ready(int err)
164 {
165 	if (err) {
166 		printk("Bluetooth init failed (err %d)\n", err);
167 		return;
168 	}
169 
170 	printk("Bluetooth initialized\n");
171 
172 	err = bt_mesh_init(&prov, &comp);
173 	if (err) {
174 		printk("Initializing mesh failed (err %d)\n", err);
175 		return;
176 	}
177 
178 	/* Initialize publication messages with dummy data */
179 	net_buf_simple_add_le32(vnd_pub.msg, UINT32_MAX);
180 	net_buf_simple_add_le32(vnd_pub2.msg, UINT32_MAX);
181 
182 	bt_mesh_prov_enable(BT_MESH_PROV_ADV | BT_MESH_PROV_GATT);
183 
184 	printk("Mesh initialized\n");
185 }
186 
main(void)187 int main(void)
188 {
189 	int err;
190 
191 	printk("Initializing...\n");
192 
193 	/* Initialize the Bluetooth Subsystem */
194 	err = bt_enable(bt_ready);
195 	if (err) {
196 		printk("Bluetooth init failed (err %d)\n", err);
197 	}
198 	return 0;
199 }
200