1 /*
2  * Copyright 2021 HIMSA II K/S - www.himsa.com.
3  * Represented by EHIMA - www.ehima.com
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 #include <list>
20 #include <vector>
21 
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24 
25 namespace bluetooth {
26 namespace groups {
27 
28 static constexpr int kGroupUnknown = -1;
29 static const bluetooth::Uuid kGenericContextUuid =
30     bluetooth::Uuid::From16Bit(0x0000);
31 
32 class DeviceGroupsCallbacks {
33  public:
34   virtual ~DeviceGroupsCallbacks() = default;
35 
36   /* Notifies first group appearance.
37    * This callback also contains first group member and uuid of the group.
38    */
39   virtual void OnGroupAdded(const RawAddress& address,
40                             const bluetooth::Uuid& group_uuid,
41                             int group_id) = 0;
42 
43   /* Followed group members are notified with this callback */
44   virtual void OnGroupMemberAdded(const RawAddress& address, int group_id) = 0;
45 
46   /* Group removal callback */
47   virtual void OnGroupRemoved(const bluetooth::Uuid& group_uuid,
48                               int group_id) = 0;
49 
50   /* Callback with group status update */
51   virtual void OnGroupMemberRemoved(const RawAddress& address,
52                                     int group_id) = 0;
53 
54   /* Callback with group information added from storage */
55   virtual void OnGroupAddFromStorage(const RawAddress& address,
56                                      const bluetooth::Uuid& group_uuid,
57                                      int group_id) = 0;
58 };
59 
60 class DeviceGroups {
61  public:
62   virtual ~DeviceGroups() = default;
63   static void Initialize(DeviceGroupsCallbacks* callbacks);
64   static void AddFromStorage(const RawAddress& addr,
65                              const std::vector<uint8_t>& in);
66   static bool GetForStorage(const RawAddress& addr, std::vector<uint8_t>& out);
67   static void CleanUp(DeviceGroupsCallbacks* callbacks);
68   static DeviceGroups* Get();
69   static void DebugDump(int fd);
70   /** To add to the existing group, group_id needs to be provided.
71    *  Otherwise a new group for the given context uuid will be created.
72    */
73   virtual int AddDevice(
74       const RawAddress& addr,
75       bluetooth::Uuid uuid = bluetooth::groups::kGenericContextUuid,
76       int group_id = bluetooth::groups::kGroupUnknown) = 0;
77   virtual int GetGroupId(
78       const RawAddress& addr,
79       bluetooth::Uuid uuid = bluetooth::groups::kGenericContextUuid) const = 0;
80   virtual void RemoveDevice(
81       const RawAddress& addr,
82       int group_id = bluetooth::groups::kGroupUnknown) = 0;
83 };
84 
85 }  // namespace groups
86 }  // namespace bluetooth
87