1/*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef OHOS_CLOUD_OAID_SERVICES_H
17#define OHOS_CLOUD_OAID_SERVICES_H
18
19#include <mutex>
20#include <string>
21
22#include "iremote_proxy.h"
23#include "distributed_kv_data_manager.h"
24#include "securec.h"
25#include "system_ability.h"
26#include "oaid_service_stub.h"
27
28namespace OHOS {
29namespace Cloud {
30enum class ServiceRunningState { STATE_NOT_START, STATE_RUNNING };
31
32class OAIDService : public SystemAbility, public OAIDServiceStub {
33    DECLARE_SYSTEM_ABILITY(OAIDService);
34
35public:
36    DISALLOW_COPY_AND_MOVE(OAIDService);
37    OAIDService(int32_t systemAbilityId, bool runOnCreate);
38    static sptr<OAIDService> GetInstance();
39    OAIDService();
40    bool InitOaidKvStore();
41    virtual ~OAIDService() override;
42
43    /**
44     * Get OAID
45     *
46     * @return std::string, OAID.
47     */
48    std::string GetOAID() override;
49
50    /**
51     * Reset open advertising id.
52     */
53    int32_t ResetOAID() override;
54
55protected:
56    void OnStart() override;
57    void OnStop() override;
58    void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
59private:
60    int32_t Init();
61    bool CheckKvStore();
62    bool ReadValueFromKvStore(const std::string &kvStoreKey, std::string &kvStoreValue);
63    bool WriteValueToKvStore(const std::string &kvStoreKey, const std::string &kvStoreValue);
64    std::string GainOAID();
65
66    ServiceRunningState state_;
67    static std::mutex mutex_;
68    static sptr<OAIDService> instance_;
69
70    std::shared_ptr<DistributedKv::SingleKvStore> oaidKvStore_;
71    std::mutex updateMutex_;
72    std::string oaid_;
73};
74} // namespace Cloud
75} // namespace OHOS
76#endif // OHOS_CLOUD_OAID_SERVICES_H
77