|
|
@ -9,36 +9,51 @@ using namespace iflytop; |
|
|
|
/***********************************************************************************************************************
|
|
|
|
* MARCO * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
#define TABLE_VERSION (TABLE_BASE_VERSION + 2)
|
|
|
|
#define TABLE_VERSION (TABLE_BASE_VERSION + 4)
|
|
|
|
#define TABLE_NAME "user_table"
|
|
|
|
|
|
|
|
/***********************************************************************************************************************
|
|
|
|
* STATIC VAR * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
|
|
|
|
static zhdb_table_t* table; |
|
|
|
static bool module_inited; |
|
|
|
static struct { |
|
|
|
typedef struct { |
|
|
|
uint32_t version; |
|
|
|
uint32_t maxid; |
|
|
|
user_t user[MAX_USR_NUM]; |
|
|
|
uint32_t end; |
|
|
|
}* tabledata; |
|
|
|
} user_table_t; |
|
|
|
|
|
|
|
static zhdb_table_t* table; |
|
|
|
static bool module_inited; |
|
|
|
static user_table_t* usr_table_data; |
|
|
|
|
|
|
|
/***********************************************************************************************************************
|
|
|
|
* FUNCTION * |
|
|
|
***********************************************************************************************************************/ |
|
|
|
|
|
|
|
static void storgeTableData() { ZHDB::storageData((uint8_t*)tabledata, sizeof(*tabledata)); } |
|
|
|
static void storgeTableData() { ZHDB::storageData((uint8_t*)usr_table_data, sizeof(*usr_table_data)); } |
|
|
|
|
|
|
|
static void cpyUsr(user_t* dst, user_t* src) { memcpy(dst, src, sizeof(user_t)); } |
|
|
|
|
|
|
|
static void sortUsr() { |
|
|
|
//
|
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (!usr_table_data->user[i].effective) { |
|
|
|
usr_table_data->user[i].id = UINT32_MAX; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 按照ID顺序排序
|
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
for (int j = i + 1; j < MAX_USR_NUM; j++) { |
|
|
|
if (tabledata->user[i].id > tabledata->user[j].id) { |
|
|
|
user_t tmp = tabledata->user[i]; |
|
|
|
tabledata->user[i] = tabledata->user[j]; |
|
|
|
tabledata->user[j] = tmp; |
|
|
|
if (usr_table_data->user[i].id > usr_table_data->user[j].id) { |
|
|
|
// user_t tmp = usr_table_data->user[i];
|
|
|
|
// usr_table_data->user[i] = usr_table_data->user[j];
|
|
|
|
// usr_table_data->user[j] = tmp;
|
|
|
|
user_t tmp; |
|
|
|
cpyUsr(&tmp, &usr_table_data->user[i]); |
|
|
|
cpyUsr(&usr_table_data->user[i], &usr_table_data->user[j]); |
|
|
|
cpyUsr(&usr_table_data->user[j], &tmp); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -46,44 +61,44 @@ static void sortUsr() { |
|
|
|
|
|
|
|
//
|
|
|
|
void UserDao::init() { //
|
|
|
|
table = ZHDB::allocTable(TABLE_NAME, sizeof(*tabledata)); |
|
|
|
table = ZHDB::allocTable(TABLE_NAME, sizeof(*usr_table_data)); |
|
|
|
ASSERT(table != NULL, "alloc table failed"); |
|
|
|
tabledata = (decltype(tabledata))table->add; |
|
|
|
usr_table_data = (decltype(usr_table_data))table->add; |
|
|
|
|
|
|
|
if (TABLE_VERSION != tabledata->version || TABLE_VERSION != tabledata->end) { |
|
|
|
if (TABLE_VERSION != usr_table_data->version || TABLE_VERSION != usr_table_data->end) { |
|
|
|
ZLOGI(TAG, "%s table version not match, reset table", TABLE_NAME); |
|
|
|
|
|
|
|
tabledata->version = TABLE_VERSION; |
|
|
|
tabledata->end = TABLE_VERSION; |
|
|
|
usr_table_data->version = TABLE_VERSION; |
|
|
|
usr_table_data->end = TABLE_VERSION; |
|
|
|
|
|
|
|
tabledata->maxid = 0; |
|
|
|
usr_table_data->maxid = 0; |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
tabledata->user[i].effective = false; |
|
|
|
tabledata->user[i].id = 0; |
|
|
|
usr_table_data->user[i].effective = false; |
|
|
|
usr_table_data->user[i].id = 0; |
|
|
|
} |
|
|
|
// User0 默认管理员
|
|
|
|
tabledata->user[0].id = tabledata->maxid++; |
|
|
|
tabledata->user[0].effective = true; |
|
|
|
strcpy(tabledata->user[0].name, "admin"); |
|
|
|
strcpy(tabledata->user[0].passwd, "888888"); |
|
|
|
tabledata->user[0].role = kadmin; |
|
|
|
tabledata->user[0].builtIn = true; |
|
|
|
usr_table_data->user[0].id = usr_table_data->maxid++; |
|
|
|
usr_table_data->user[0].effective = true; |
|
|
|
strcpy(usr_table_data->user[0].name, "admin"); |
|
|
|
strcpy(usr_table_data->user[0].passwd, "888888"); |
|
|
|
usr_table_data->user[0].role = kadmin; |
|
|
|
usr_table_data->user[0].builtIn = true; |
|
|
|
|
|
|
|
// User1 默认仓库管理员
|
|
|
|
// tabledata->user[1].id = tabledata->maxid++;
|
|
|
|
// tabledata->user[1].effective = true;
|
|
|
|
// strcpy(tabledata->user[1].name, "manager");
|
|
|
|
// strcpy(tabledata->user[1].passwd, "888888");
|
|
|
|
// tabledata->user[1].role = kstorekeeper;
|
|
|
|
// tabledata->user[1].builtIn = true;
|
|
|
|
usr_table_data->user[1].id = usr_table_data->maxid++; |
|
|
|
usr_table_data->user[1].effective = true; |
|
|
|
strcpy(usr_table_data->user[1].name, "manager"); |
|
|
|
strcpy(usr_table_data->user[1].passwd, "888888"); |
|
|
|
usr_table_data->user[1].role = kstorekeeper; |
|
|
|
usr_table_data->user[1].builtIn = false; |
|
|
|
|
|
|
|
// User2 默认操作员
|
|
|
|
// tabledata->user[2].id = tabledata->maxid++;
|
|
|
|
// tabledata->user[2].effective = true;
|
|
|
|
// strcpy(tabledata->user[2].name, "用户");
|
|
|
|
// strcpy(tabledata->user[2].passwd, "888888");
|
|
|
|
// tabledata->user[2].role = kuser;
|
|
|
|
// tabledata->user[2].builtIn = false;
|
|
|
|
usr_table_data->user[2].id = usr_table_data->maxid++; |
|
|
|
usr_table_data->user[2].effective = true; |
|
|
|
strcpy(usr_table_data->user[2].name, "用户"); |
|
|
|
strcpy(usr_table_data->user[2].passwd, "888888"); |
|
|
|
usr_table_data->user[2].role = kuser; |
|
|
|
usr_table_data->user[2].builtIn = false; |
|
|
|
} |
|
|
|
storgeTableData(); |
|
|
|
module_inited = true; |
|
|
@ -92,21 +107,21 @@ void UserDao::init() { // |
|
|
|
} |
|
|
|
|
|
|
|
void UserDao::getUsers(user_t** user, int* numUser) { |
|
|
|
*user = tabledata->user; |
|
|
|
*user = usr_table_data->user; |
|
|
|
int count = 0; |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective) { |
|
|
|
if (usr_table_data->user[i].effective) { |
|
|
|
count++; |
|
|
|
} |
|
|
|
} |
|
|
|
*numUser = count; |
|
|
|
} |
|
|
|
|
|
|
|
user_t* UserDao::getUsersTable() { return tabledata->user; } |
|
|
|
user_t* UserDao::getUsersTable() { return usr_table_data->user; } |
|
|
|
int UserDao::getUsersTableNum() { |
|
|
|
int count = 0; |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective) { |
|
|
|
if (usr_table_data->user[i].effective) { |
|
|
|
count++; |
|
|
|
} |
|
|
|
} |
|
|
@ -116,8 +131,8 @@ int UserDao::getUsersTableNum() { |
|
|
|
user_t* UserDao::getUserByName(const char* name) { |
|
|
|
ZASSERT_INFO(module_inited, "user dao not inited"); |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective && strcmp(tabledata->user[i].name, name) == 0) { |
|
|
|
return &tabledata->user[i]; |
|
|
|
if (usr_table_data->user[i].effective && strcmp(usr_table_data->user[i].name, name) == 0) { |
|
|
|
return &usr_table_data->user[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
return nullptr; |
|
|
@ -125,8 +140,8 @@ user_t* UserDao::getUserByName(const char* name) { |
|
|
|
user_t* UserDao::getUserById(int32_t id) { |
|
|
|
ZASSERT_INFO(module_inited, "user dao not inited"); |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective && tabledata->user[i].id == id) { |
|
|
|
return &tabledata->user[i]; |
|
|
|
if (usr_table_data->user[i].effective && usr_table_data->user[i].id == id) { |
|
|
|
return &usr_table_data->user[i]; |
|
|
|
} |
|
|
|
} |
|
|
|
return nullptr; |
|
|
@ -144,8 +159,8 @@ Errno UserDao::addUser(const char* name, const char* passwd, user_role_t role) { |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (!tabledata->user[i].effective) { |
|
|
|
user = &tabledata->user[i]; |
|
|
|
if (!usr_table_data->user[i].effective) { |
|
|
|
user = &usr_table_data->user[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -153,12 +168,15 @@ Errno UserDao::addUser(const char* name, const char* passwd, user_role_t role) { |
|
|
|
ZLOGE(TAG, "no space for new user"); |
|
|
|
return Errno::ERR_USER_SPACE_FULL; |
|
|
|
} |
|
|
|
user->id = tabledata->maxid++; |
|
|
|
user->id = usr_table_data->maxid++; |
|
|
|
user->effective = true; |
|
|
|
user->builtIn = false; |
|
|
|
strcpy(user->name, name); |
|
|
|
strcpy(user->passwd, passwd); |
|
|
|
user->role = role; |
|
|
|
|
|
|
|
ZLOGI(TAG, "add user %s %d", name, user->id); |
|
|
|
|
|
|
|
sortUsr(); |
|
|
|
storgeTableData(); |
|
|
|
return Errno::OK; |
|
|
@ -179,11 +197,12 @@ const char* createNewUsrName() { |
|
|
|
Errno UserDao::addUser() { return UserDao::addUser(createNewUsrName(), "888888", kuser); } |
|
|
|
|
|
|
|
void UserDao::delUser(uint8_t userId) { |
|
|
|
ZLOGI(TAG, "del user %d", userId); |
|
|
|
ZASSERT_INFO(module_inited, "user dao not inited"); |
|
|
|
|
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective && tabledata->user[i].id == userId) { |
|
|
|
tabledata->user[i].effective = false; |
|
|
|
if (usr_table_data->user[i].effective && usr_table_data->user[i].id == userId) { |
|
|
|
usr_table_data->user[i].effective = false; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
@ -196,8 +215,8 @@ void UserDao::updateUser(uint8_t userId, const char* name, const char* passwd, u |
|
|
|
|
|
|
|
user_t* user = nullptr; |
|
|
|
for (int i = 0; i < MAX_USR_NUM; i++) { |
|
|
|
if (tabledata->user[i].effective && tabledata->user[i].id == userId) { |
|
|
|
user = &tabledata->user[i]; |
|
|
|
if (usr_table_data->user[i].effective && usr_table_data->user[i].id == userId) { |
|
|
|
user = &usr_table_data->user[i]; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|