1 /*
2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3 *
4 * HDF is dual licensed: you can use it either under the terms of
5 * the GPL, or the BSD license, at your option.
6 * See the LICENSE file in the root of this repository for complete details.
7 */
8
9 #include "hdf_observer_record.h"
10 #include "hdf_log.h"
11 #include "hdf_service_subscriber.h"
12 #include "osal_mem.h"
13
14 #define HDF_LOG_TAG observer_record
15 #define HALF_INT_LEN 16
16
HdfServiceObserverRecordObtain(uint32_t serviceKey)17 struct HdfServiceObserverRecord *HdfServiceObserverRecordObtain(uint32_t serviceKey)
18 {
19 struct HdfServiceObserverRecord *observerRecord =
20 (struct HdfServiceObserverRecord *)OsalMemCalloc(sizeof(struct HdfServiceObserverRecord));
21 if (observerRecord != NULL) {
22 observerRecord->serviceKey = serviceKey;
23 observerRecord->publisher = NULL;
24 if (OsalMutexInit(&observerRecord->obsRecMutex) != HDF_SUCCESS) {
25 OsalMemFree(observerRecord);
26 return NULL;
27 }
28 HdfSListInit(&observerRecord->subscribers);
29 }
30 return observerRecord;
31 }
32
HdfServiceObserverRecordRecycle(struct HdfServiceObserverRecord * observerRecord)33 void HdfServiceObserverRecordRecycle(struct HdfServiceObserverRecord *observerRecord)
34 {
35 if (observerRecord != NULL) {
36 HdfSListFlush(&observerRecord->subscribers, HdfServiceSubscriberDelete);
37 OsalMutexDestroy(&observerRecord->obsRecMutex);
38 observerRecord->obsRecMutex.realMutex = NULL;
39 OsalMemFree(observerRecord);
40 }
41 }
42
HdfServiceObserverRecordCompare(struct HdfSListNode * listEntry,uint32_t serviceKey)43 bool HdfServiceObserverRecordCompare(struct HdfSListNode *listEntry, uint32_t serviceKey)
44 {
45 struct HdfServiceObserverRecord *record = NULL;
46 if (listEntry == NULL) {
47 return false;
48 }
49 record = (struct HdfServiceObserverRecord *)listEntry;
50 if (record->serviceKey == serviceKey) {
51 return true;
52 }
53 return false;
54 }
55
HdfServiceObserverRecordNotifySubscribers(struct HdfServiceObserverRecord * record,devid_t deviceId,uint16_t policy)56 void HdfServiceObserverRecordNotifySubscribers(
57 struct HdfServiceObserverRecord *record, devid_t deviceId, uint16_t policy)
58 {
59 struct HdfSListIterator it;
60 if (record == NULL) {
61 HDF_LOGE("%s: record is null", __func__);
62 return;
63 }
64
65 OsalMutexLock(&record->obsRecMutex);
66 HdfSListIteratorInit(&it, &record->subscribers);
67 while (HdfSListIteratorHasNext(&it)) {
68 struct HdfServiceSubscriber *subscriber =
69 (struct HdfServiceSubscriber *)HdfSListIteratorNext(&it);
70 if (deviceId == subscriber->devId || policy != SERVICE_POLICY_PRIVATE) {
71 subscriber->state = HDF_SUBSCRIBER_STATE_READY;
72 if (subscriber->callback.OnServiceConnected != NULL) {
73 subscriber->callback.OnServiceConnected(subscriber->callback.deviceObject, record->publisher);
74 }
75 }
76 }
77 OsalMutexUnlock(&record->obsRecMutex);
78 }
79
HdfServiceObserverRecordDelete(struct HdfSListNode * listEntry)80 void HdfServiceObserverRecordDelete(struct HdfSListNode *listEntry)
81 {
82 struct HdfServiceObserverRecord *observerRecord = (struct HdfServiceObserverRecord *)listEntry;
83 if (observerRecord != NULL) {
84 HdfServiceObserverRecordRecycle(observerRecord);
85 }
86 }
87
88