mcu_hi3321_watch/tjd/ui/app/phone/phone_service/TjdPhoneContacts.cpp
2025-05-26 20:15:20 +08:00

80 lines
2.2 KiB
C++

/*----------------------------------------------------------------------------
* Copyright (c) TJD Technologies Co., Ltd. 2024. All rights reserved.
*
* Description: TjdPhoneContacts.cpp
*
* Author: luziquan@ss-tjd.com
*
* Create: 2024-10-29
*--------------------------------------------------------------------------*/
#include "TjdPhoneContacts.h"
#include "service_bt.h"
#include "sys_config.h"
#define ENABLE_PRINT_INFO 1
#if ENABLE_PRINT_INFO
#define static_print_warn(...) sys_phone_log_w(__VA_ARGS__) // 警告信息打印一般常开
#define static_print_error(...) sys_phone_log_e(__VA_ARGS__) // 错误信息打印一般常开
#define static_print_debug(...) sys_phone_log_d(__VA_ARGS__) // 调试信息打印一般常开
#else
#define static_print_warn(...)
#define static_print_error(...)
#define static_print_debug(...)
#endif
#define TJD_CONTACTS_PATH "/user/tjd_phone/addressbook.bin"
std::vector<ContactInfo> g_contacts;
const std::vector<ContactInfo> &GetContacts(void) { return g_contacts; }
bool ContactsGetCase(void)
{
g_contacts.clear();
FILE *fp = fopen(TJD_CONTACTS_PATH, "rb");
if (fp == nullptr) {
static_print_error("[ReadContacts] fopen %s failed!", TJD_CONTACTS_PATH);
return false;
}
Contact contact;
while (fread(&contact, sizeof(Contact), 1, fp) == 1) {
ContactInfo contactInfo = {contact.name, contact.phone};
g_contacts.emplace_back(contactInfo);
}
if (ferror(fp)) {
static_print_error("[ReadContacts] fread failed!");
}
fclose(fp);
return true;
}
const ContactInfo *FindContactOfNumber(const unsigned char *number, unsigned char numberLen)
{
if (g_contacts.empty()) {
return nullptr;
}
for (auto &contact : g_contacts) {
if (contact.number.size() == numberLen && contact.number.compare((const char*)number) == 0) {
return &contact;
}
}
return nullptr;
}
const ContactInfo *FindContactOfName(const unsigned char *name, unsigned char nameLen)
{
if (g_contacts.empty()) {
return nullptr;
}
for (auto &contact : g_contacts) {
if (contact.name.size() == nameLen && contact.name.compare((const char*)name) == 0) {
return &contact;
}
}
return nullptr;
}