~aleteoryx/muditaos

ref: 40fa31a954b10383da6cee6b98c06b5fb932d266 muditaos/module-services/service-cellular/src/SimContacts.cpp -rw-r--r-- 2.1 KiB
40fa31a9 — Maciej-Mudita [MOS-827] Fix looping on the SIM card selection screen 3 years ago
                                                                                
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
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include "SimContacts.hpp"

#include <modem/BaseChannel.hpp>
#include <at/cmd/CPBS.hpp>
#include <at/cmd/CPBR.hpp>

namespace cellular::service
{
    void SimContacts::setChannel(at::BaseChannel *channel)
    {
        this->channel = channel;
    }

    auto SimContacts::getContacts(std::vector<cellular::SimContact> &destination) -> bool
    {
        constexpr auto firstIndex = 1;
        if (channel == nullptr) {
            LOG_ERROR("No channel provided. Request ignored");
            return false;
        }
        auto contactsCount = 0;
        if (!getContactCount(contactsCount)) {
            return false;
        }

        auto command = at::cmd::CPBR(at::cmd::Modifier::Set);
        command.setSimContactsReadRange(firstIndex, contactsCount);
        auto response = channel->cmd(command);
        auto result   = command.parseCPBR(response);

        if (result.code != at::Result::Code::OK) {
            LOG_ERROR("Failed to get contacts list");
            return false;
        }

        for (auto el : result.contacts) {
            destination.push_back(SimContact(el.name, el.number));
        }
        return true;
    }

    auto SimContacts::getContactCount(int &count) -> bool
    {
        if (channel == nullptr) {
            LOG_ERROR("No channel provided. Request ignored");
            return false;
        }

        auto command = at::cmd::CPBS(at::cmd::Modifier::Set);
        command.set();
        auto response = channel->cmd(command);

        if (response.code != at::Result::Code::OK) {
            LOG_ERROR("Failed to set contact storage");
            return false;
        }

        command     = at::cmd::CPBS(at::cmd::Modifier::Get);
        response    = channel->cmd(command);
        auto result = command.parseCPBS(response);

        if (result.code != at::Result::Code::OK) {
            LOG_ERROR("Failed to get contacts count");
            return false;
        }

        count = result.used;
        return true;
    }
} // namespace cellular::service