Blob: prvbeac-server.c

Blob id: f318852b8a42b804e4fa5e8ce7f8b57facaec5ee

Size: 2.9 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2023  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.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/time.h>
#include <time.h>
#include <ell/ell.h>

#include "mesh/mesh-defs.h"
#include "mesh/node.h"
#include "mesh/net.h"
#include "mesh/appkey.h"
#include "mesh/model.h"
#include "mesh/mesh-config.h"
#include "mesh/prv-beacon.h"

#define NOT_SUPPORTED 0x02

static bool prvbec_srv_pkt(uint16_t src, uint16_t dst, uint16_t app_idx,
				uint16_t net_idx, const uint8_t *data,
				uint16_t size, const void *user_data)
{
	struct mesh_node *node = (struct mesh_node *) user_data;
	const uint8_t *pkt = data;
	uint32_t opcode;
	uint8_t msg[5];
	uint16_t n;
	uint8_t period;

	if (app_idx != APP_IDX_DEV_LOCAL)
		return false;

	if (mesh_model_opcode_get(pkt, size, &opcode, &n)) {
		size -= n;
		pkt += n;
	} else
		return false;

	l_debug("PRV-BEAC-SRV-opcode 0x%x size %u idx %3.3x", opcode, size,
								net_idx);

	n = 0;

	switch (opcode) {
	default:
		return false;

	case OP_PRIVATE_BEACON_SET:
		if (size == 1)
			node_mpb_mode_get(node, &period);
		else if (size == 2)
			period = pkt[1];
		else
			return true;

		if (pkt[0] > 1)
			return true;

		node_mpb_mode_set(node, !!pkt[0], period);

		/* fall through */

	case OP_PRIVATE_BEACON_GET:
		n = mesh_model_opcode_set(OP_PRIVATE_BEACON_STATUS, msg);

		msg[n++] = node_mpb_mode_get(node, &period);
		msg[n++] = period;

		l_debug("Get/Set Private Beacon (%d)", msg[n-2]);
		break;

	case OP_PRIVATE_GATT_PROXY_SET:
		/* fall through */
	case OP_PRIVATE_GATT_PROXY_GET:
		n = mesh_model_opcode_set(OP_PRIVATE_GATT_PROXY_STATUS, msg);
		msg[n++] = NOT_SUPPORTED;
		break;

	case OP_PRIVATE_NODE_ID_SET:
		/* fall through */
	case OP_PRIVATE_NODE_ID_GET:
		n = mesh_model_opcode_set(OP_PRIVATE_NODE_ID_STATUS, msg);
		msg[n++] = NOT_SUPPORTED;
		break;
	}

	if (n)
		mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx,
						DEFAULT_TTL, false, n, msg);

	return true;
}

static void prvbec_srv_unregister(void *user_data)
{
}

static const struct mesh_model_ops ops = {
	.unregister = prvbec_srv_unregister,
	.recv = prvbec_srv_pkt,
	.bind = NULL,
	.sub = NULL,
	.pub = NULL
};

void prv_beacon_server_init(struct mesh_node *node, uint8_t ele_idx)
{
	l_debug("%2.2x", ele_idx);
	mesh_model_register(node, ele_idx, PRV_BEACON_SRV_MODEL, &ops, node);
}