You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
1443 lines
59 KiB
1443 lines
59 KiB
|
|
#include "radar_driver.h" |
|
#include "dbscan.c" // Ken 2022-10-24 |
|
#include "radar_parser.h" |
|
#include "cryptionPlus.h" |
|
|
|
char g_stream_state_response[256] = {0}; |
|
radarconvert union_radar_convert; |
|
|
|
RadarServiceInfo radar_service_data; |
|
RadarDataInfo radar_data_array[MAX_SIZE_RADAR_DATA] = {0}; |
|
|
|
RadarDataInfo world_radar_data_array[MAX_SIZE_RADAR_DATA] = {0}; // leo 09-29 |
|
int g_current_radar_data_index = 0; |
|
|
|
int g_check_if_radar_start = 0; |
|
int g_check_if_is_correct_ttype = 0; |
|
|
|
int g_switch_radar_data_json = 0; |
|
char g_radar_data_json[BUFSIZE_V3] = {0}; |
|
#ifdef RADAR_LOG |
|
int start_radar_log_header = 1; // 10-19 leo |
|
int radar_log = 0; // 10-19 leo |
|
char radar_csv_name[128]; |
|
#endif // DEBUG |
|
|
|
// 10-24 Ken DBSCAN param |
|
int radar_dbscan = 0; |
|
int radar_type_1_points_num = 0; |
|
double epsilon = 2; |
|
unsigned int minpts = 1; |
|
point_t *points = NULL; |
|
|
|
int g_old_magic_num; |
|
|
|
void run_general_ttype(char *buffer_stream, int buffer_size) |
|
{ |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>> run_general_ttype_P :%d, %d\n", buffer_size, (READ_OBJ_AND_VUNIT_P * INT_BYTE_SIZE_P)); |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>> type = %ld \n", radar_data_array[g_current_radar_data_index].ttype); |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>> [%d] TLVLen = %ld - %ld\n", g_current_radar_data_index, radar_data_array[g_current_radar_data_index].TLVLength, radar_service_data.totalPacketLen); |
|
if (buffer_size >= READ_OBJ_AND_VUNIT * INT_BYTE_SIZE && |
|
radar_data_array[g_current_radar_data_index].ttype >= 1 && |
|
radar_data_array[g_current_radar_data_index].ttype <= 3 && |
|
// radar_data_array[g_current_radar_data_index].ttype <= 4 && |
|
radar_data_array[g_current_radar_data_index].TLVLength < radar_service_data.totalPacketLen && |
|
radar_data_array[g_current_radar_data_index].TLVLength >= 1) |
|
{ |
|
int objs = 0; |
|
float vUnit = 0.0; |
|
|
|
for (int i = 0; i < READ_OBJ_AND_VUNIT; i++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + i * 2, 2); |
|
if (i == 0) |
|
objs = (int)union_radar_convert.int16; |
|
else if (i == 1) |
|
vUnit = 1.0 / (float)pow(2.0, (double)(int)union_radar_convert.int16); |
|
} |
|
printf("\n--objs:%d,vunit:%f\n", objs, vUnit); |
|
|
|
//printf("-------------------------------------------------------- radar objs: %d \n", objs); |
|
|
|
radar_type_1_points_num = 0; // 10-24 Ken |
|
// printf("radar_type_1_points_num : %d\n", radar_type_1_points_num); |
|
|
|
if (objs >= 1 && vUnit > 0.0) |
|
{ |
|
int ori_g_current_radar_data_index = g_current_radar_data_index; |
|
|
|
if (radar_data_array[g_current_radar_data_index].ttype == 1) |
|
{ |
|
if (buffer_size == (READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + objs * TTYPE_1_OBJ_FORMAT_SIZE * INT_BYTE_SIZE)) |
|
{ |
|
|
|
for (int i = 0; i < objs; i++) |
|
{ |
|
if (g_current_radar_data_index != ori_g_current_radar_data_index) |
|
{ |
|
radar_data_array[g_current_radar_data_index].ttype = radar_data_array[ori_g_current_radar_data_index].ttype; |
|
radar_data_array[g_current_radar_data_index].TLVLength = radar_data_array[ori_g_current_radar_data_index].TLVLength; |
|
} |
|
|
|
float doppler = 0.0, PeakValue = 0.0, X = 0.0, Y = 0.0, Z = 0.0; // for V1 |
|
// float ccX = 0.0, ccY = 0.0, csX = 0.0, csY = 0.0; //for V2 |
|
// float tX = 0.0, tY = 0.0, velX = 0.0, velY = 0.0; //for V3 |
|
|
|
for (int j = 0; j < TTYPE_1_OBJ_FORMAT_SIZE; j++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + i * TTYPE_1_OBJ_FORMAT_SIZE + j * 2, 2); |
|
if (j == 0) |
|
doppler = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 1) |
|
PeakValue = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 2) |
|
X = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 3) |
|
Y = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 4) |
|
Z = vUnit * (float)(int)union_radar_convert.int16; |
|
} |
|
|
|
// printf("\n---value:%f,%f,%f,%f,%f\n", doppler, PeakValue, X, Y, Z); |
|
|
|
radar_data_array[g_current_radar_data_index].doppler = doppler; |
|
radar_data_array[g_current_radar_data_index].PeakValue = PeakValue; |
|
radar_data_array[g_current_radar_data_index].X = X; |
|
radar_data_array[g_current_radar_data_index].Y = Y; |
|
radar_data_array[g_current_radar_data_index].Z = Z; |
|
|
|
g_current_radar_data_index++; |
|
radar_type_1_points_num++; |
|
// printf("--- radar_type_1_points_num : %d\n", radar_type_1_points_num); |
|
} |
|
} |
|
} |
|
else if (radar_data_array[g_current_radar_data_index].ttype == 2) |
|
{ |
|
|
|
if (buffer_size == (READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + objs * TTYPE_2_OBJ_FORMAT_SIZE * INT_BYTE_SIZE)) |
|
{ |
|
|
|
for (int i = 0; i < objs; i++) |
|
{ |
|
if (g_current_radar_data_index != ori_g_current_radar_data_index) |
|
{ |
|
radar_data_array[g_current_radar_data_index].ttype = radar_data_array[ori_g_current_radar_data_index].ttype; |
|
radar_data_array[g_current_radar_data_index].TLVLength = radar_data_array[ori_g_current_radar_data_index].TLVLength; |
|
} |
|
|
|
// float doppler = 0.0, PeakValue = 0.0, X = 0.0, Y = 0.0, Z = 0.0; //for V1 |
|
float ccX = 0.0, ccY = 0.0, csX = 0.0, csY = 0.0; // for V2 |
|
// float tX = 0.0, tY = 0.0, velX = 0.0, velY = 0.0; //for V3 |
|
|
|
for (int j = 0; j < TTYPE_2_OBJ_FORMAT_SIZE; j++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + i * TTYPE_2_OBJ_FORMAT_SIZE + j * 2, 2); |
|
if (j == 0) |
|
ccX = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 1) |
|
ccY = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 2) |
|
csX = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 3) |
|
csY = vUnit * (float)(int)union_radar_convert.int16; |
|
} |
|
|
|
// printf("\n---value:%f,%f,%f,%f\n", ccX, ccY, csX, csY); |
|
|
|
radar_data_array[g_current_radar_data_index].ccX = ccX; |
|
radar_data_array[g_current_radar_data_index].ccY = ccY; |
|
radar_data_array[g_current_radar_data_index].csX = csX; |
|
radar_data_array[g_current_radar_data_index].csY = csY; |
|
|
|
g_current_radar_data_index++; |
|
} |
|
} |
|
} |
|
else if (radar_data_array[g_current_radar_data_index].ttype == 3) |
|
{ |
|
if (buffer_size == (READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + objs * TTYPE_3_OBJ_FORMAT_SIZE * INT_BYTE_SIZE)) |
|
{ |
|
|
|
for (int i = 0; i < objs; i++) |
|
{ |
|
if (g_current_radar_data_index != ori_g_current_radar_data_index) |
|
{ |
|
radar_data_array[g_current_radar_data_index].ttype = radar_data_array[ori_g_current_radar_data_index].ttype; |
|
radar_data_array[g_current_radar_data_index].TLVLength = radar_data_array[ori_g_current_radar_data_index].TLVLength; |
|
} |
|
|
|
// float doppler = 0.0, PeakValue = 0.0, X = 0.0, Y = 0.0, Z = 0.0; //for V1 |
|
/*/float ccX = 0.0, ccY = 0.0,*/ float csX = 0.0, csY = 0.0; // for V2 |
|
float tX = 0.0, tY = 0.0, velX = 0.0, velY = 0.0; // for V3 |
|
|
|
for (int j = 0; j < TTYPE_3_OBJ_FORMAT_SIZE; j++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + i * TTYPE_3_OBJ_FORMAT_SIZE + j * 2, 2); |
|
if (j == 0) |
|
tX = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 1) |
|
tY = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 2) |
|
velX = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 3) |
|
velY = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 4) |
|
csX = vUnit * (float)(int)union_radar_convert.int16; |
|
else if (j == 5) |
|
csY = vUnit * (float)(int)union_radar_convert.int16; |
|
} |
|
|
|
// printf("\n---value:%f,%f,%f,%f,%f,%f\n", tX, tY, velX, velY, csX,csY); |
|
|
|
radar_data_array[g_current_radar_data_index].tX = tX; |
|
radar_data_array[g_current_radar_data_index].tY = tY; |
|
radar_data_array[g_current_radar_data_index].velX = velX; |
|
radar_data_array[g_current_radar_data_index].velY = velY; |
|
radar_data_array[g_current_radar_data_index].csX = csX; |
|
radar_data_array[g_current_radar_data_index].csY = csY; |
|
|
|
if (tX != 0 && tY != 0) |
|
radar_data_array[g_current_radar_data_index].Speed = ((tX * velX + tY * velY) / sqrt((tX * tX + tY * tY))) * 3.6; |
|
|
|
g_current_radar_data_index++; |
|
} |
|
} |
|
} |
|
#if 0 |
|
else if (radar_data_array[g_current_radar_data_index].ttype == 4) { |
|
if (buffer_size == (READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + objs * TTYPE_4_OBJ_FORMAT_SIZE * INT_BYTE_SIZE)) { |
|
|
|
for (int i = 0; i < objs; i++) |
|
{ |
|
if (g_current_radar_data_index != ori_g_current_radar_data_index) { |
|
radar_data_array[g_current_radar_data_index].ttype = radar_data_array[ori_g_current_radar_data_index].ttype; |
|
radar_data_array[g_current_radar_data_index].TLVLength = radar_data_array[ori_g_current_radar_data_index].TLVLength; |
|
} |
|
|
|
for (int j = 0; j < TTYPE_4_OBJ_FORMAT_SIZE; j++) { |
|
memcpy(union_radar_convert.hexarray, (unsigned char*)buffer_stream + READ_OBJ_AND_VUNIT * INT_BYTE_SIZE + i * TTYPE_4_OBJ_FORMAT_SIZE + j * 2, 2); |
|
//printf("\n----ast[%d] = %d\n", i, union_radar_convert.int16); |
|
} |
|
|
|
g_current_radar_data_index++; |
|
} |
|
} |
|
} |
|
#endif |
|
|
|
#if 1 // leo 09-29 |
|
if (g_current_radar_data_index >= 1) |
|
{ |
|
// Ken 10-24 DBSCAN |
|
if (radar_data_array[g_current_radar_data_index-1].ttype == 1 && radar_dbscan == 0) |
|
{ |
|
// printf("run_general_ttype - g_current_radar_data_index>=1, if type ==1, if (radar_dbscan == 0)\n"); |
|
points = (point_t *)calloc(radar_type_1_points_num, sizeof(point_t)); |
|
|
|
for (int j = 0; j < g_current_radar_data_index; j++) |
|
{ |
|
points[j].x = radar_data_array[j].X; |
|
points[j].y = radar_data_array[j].Y; |
|
points[j].z = radar_data_array[j].Z; |
|
points[j].cluster_id = UNCLASSIFIED; |
|
points[j].nop = 1; |
|
} |
|
|
|
// printf("=== DBSCAN Settings ===\n"); |
|
// printf("Epsilon: %lf\n", epsilon); |
|
// printf("Minimum points: %u\n", minpts); |
|
dbscan(points, radar_type_1_points_num, epsilon, minpts, euclidean_dist); |
|
|
|
// printf("=== update nop START ===\n"); |
|
// TODO update nop ( number of cluster points ) |
|
int max_cluster_id = 0; |
|
|
|
for (int j = 0; j < g_current_radar_data_index; j++) |
|
{ |
|
if (points[j].cluster_id > max_cluster_id) |
|
{ |
|
max_cluster_id = points[j].cluster_id; |
|
} |
|
} |
|
|
|
for (int cluster_id_index = 0; cluster_id_index <= max_cluster_id; cluster_id_index++) |
|
{ |
|
int counter = 0; |
|
for (int j = 0; j < g_current_radar_data_index; j++) |
|
{ |
|
if (points[j].cluster_id == cluster_id_index) |
|
{ |
|
++counter; |
|
} |
|
} |
|
|
|
for (int j = 0; j < g_current_radar_data_index; j++) |
|
{ |
|
if (points[j].cluster_id == cluster_id_index) |
|
{ |
|
points[j].nop = counter; |
|
} |
|
// printf("points[%d].cluster_id : %d, .nop : %d\n", j, points[j].cluster_id, points[j].nop); |
|
} |
|
} |
|
// printf("=== update nop END ===\n"); |
|
|
|
radar_dbscan = 1; |
|
} |
|
|
|
for (int i = 0; i < g_current_radar_data_index; i++) |
|
{ |
|
radar_axis_covert_world(&radar_data_array[i], &world_radar_data_array[i]); |
|
|
|
// printf("Type %ld origin(x,y): (%f,%f) transfer(x,y): (%f,%f) \n", temp_current_radar_list[i].ttype, temp_current_radar_list[i].X, temp_current_radar_list[i].Y, g_current_radar_list[i].X, g_current_radar_list[i].Y); |
|
} |
|
} |
|
#endif |
|
} |
|
} |
|
} |
|
|
|
int Is_radar_data_empty(char *buffer_stream, int buffer_size) |
|
{ |
|
char temp_char = buffer_stream[0]; |
|
int ret = 0; |
|
|
|
for (int i = 1; i < buffer_size; i++) |
|
{ |
|
if (buffer_stream[i] != temp_char) |
|
break; |
|
else if (i == buffer_size - 1) |
|
{ |
|
ret = 1; |
|
} |
|
} |
|
return ret; |
|
} |
|
|
|
int Is_correct_ttype(char *buffer_stream, int buffer_size) |
|
{ |
|
int ret = 0; |
|
if (buffer_size == MAX_SIZE_LILIN_TTYPE * 4) // if buffer_size = 8 byte |
|
{ |
|
|
|
long temp_ttype = 0; |
|
long temp_TLVLength = 0; |
|
|
|
for (int i = 0; i < MAX_SIZE_LILIN_TTYPE; i++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + i * 4, 4); |
|
if (i == 0) |
|
temp_ttype = (long)union_radar_convert.int32; |
|
else if (i == 1) |
|
temp_TLVLength = (long)union_radar_convert.int32; |
|
} |
|
|
|
if (temp_ttype >= 1 && temp_ttype <= 4 && temp_TLVLength < radar_service_data.totalPacketLen && temp_TLVLength >= 1) |
|
{ |
|
radar_data_array[g_current_radar_data_index].ttype = temp_ttype; |
|
radar_data_array[g_current_radar_data_index].TLVLength = temp_TLVLength; |
|
|
|
ret = 1; |
|
} |
|
} |
|
return ret; |
|
} |
|
|
|
// 2022-10-26 Ken |
|
int rule_based_classifier(point_t *p, int p_ind, float doppler, int class_id) |
|
{ |
|
// GYNet_Radar_Tiny_Label: |
|
// 0 : person |
|
// 1 : bicycle |
|
// 2 : car |
|
// 3 : motorbike |
|
// 4 : _s |
|
// 5 : bus |
|
// 6 : _s |
|
// 7 : truck |
|
|
|
float speed = -doppler*3600.0/1000.0; |
|
|
|
float distance = euclidean_dist_to_op(&p[p_ind]); |
|
|
|
int nop = p[p_ind].nop; |
|
|
|
// rules |
|
if (speed >= 82.8) |
|
{ |
|
class_id = -1; |
|
} |
|
else if (distance < 40 && distance >= 1 && nop <= 15 && nop >= 1) |
|
{ |
|
class_id = 0; // person |
|
} |
|
// ... |
|
|
|
|
|
// printf("--- p_ind: %d, distance:%f, speed:%f, class_id:%f\n", p_ind, distance, speed, class_id); |
|
|
|
|
|
return class_id; |
|
} |
|
|
|
void build_radar_data_json() |
|
{ |
|
FILE *fp = NULL; |
|
#ifdef RADAR_LOG |
|
// 10-19 leo |
|
if (start_radar_log_header == 1 && radar_log == 1) |
|
{ |
|
start_radar_log_header = 0; |
|
time_t t = time(NULL); |
|
struct tm tm = *localtime(&t); |
|
printf("now: %d-%02d-%02d %02d:%02d:%02d\n", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); |
|
|
|
sprintf(radar_csv_name, "/emmc/plugin/Aida_data/radar/radar_log_%d%02d%02d_%02d%02d.csv", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min); |
|
printf("%s\n", radar_csv_name); |
|
fp = fopen(radar_csv_name, "w+"); |
|
if (fp == NULL) |
|
{ |
|
fprintf(stderr, "fopen() failed.\n"); |
|
radar_log = 0; |
|
} |
|
// fprintf(fp, "FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak\n"); |
|
fprintf(fp, "FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak, cluster_id\n"); |
|
} |
|
else if (start_radar_log_header == 0 && radar_log == 1) |
|
{ |
|
fp = fopen(radar_csv_name, "a"); |
|
if (fp == NULL) |
|
{ |
|
fprintf(stderr, "fopen() failed.\n"); |
|
radar_log = 0; |
|
} |
|
} |
|
#endif // RADAR_LOG |
|
cJSON *root = NULL; |
|
cJSON *AiRadar, *detectArrayRadar, *detectRadar; |
|
|
|
/* |
|
cJSON *FrameNum, *tid, *posX, *posY, *posZ, *velX, *velY, *velZ, |
|
*accX, *accY, *accZ, *ccX, *ccY, *csX, *csY, *tX, *tY, *Doppler; |
|
*/ |
|
|
|
root = cJSON_CreateObject(); |
|
|
|
AiRadar = cJSON_CreateObject(); |
|
cJSON_AddItemToObject(root, "AiRadar", AiRadar); |
|
|
|
time_t current_time = time(0); |
|
char c_current_time[20] = {0}; |
|
sprintf(c_current_time, "%ld", current_time); |
|
cJSON_AddItemToObject(AiRadar, "Time", cJSON_CreateString(c_current_time)); |
|
|
|
char c_framenum[20] = {0}; |
|
sprintf(c_framenum, "%ld", radar_service_data.frameNumber); |
|
cJSON_AddItemToObject(AiRadar, "FrameNum", cJSON_CreateString(c_framenum)); |
|
|
|
char c_radar_x_axis[20] = {0}; |
|
sprintf(c_radar_x_axis, "%d", json_radar_x_axis); |
|
cJSON_AddItemToObject(AiRadar, "radar_x_axis", cJSON_CreateString(c_radar_x_axis)); |
|
|
|
char c_radar_y_axis[20] = {0}; |
|
sprintf(c_radar_y_axis, "%d", json_radar_y_axis); |
|
cJSON_AddItemToObject(AiRadar, "radar_y_axis", cJSON_CreateString(c_radar_y_axis)); |
|
|
|
char c_target_x_axis[20] = {0}; |
|
sprintf(c_target_x_axis, "%d", json_target_x_axis); |
|
cJSON_AddItemToObject(AiRadar, "target_x_axis", cJSON_CreateString(c_target_x_axis)); |
|
|
|
char c_target_y_axis[20] = {0}; |
|
sprintf(c_target_y_axis, "%d", json_target_y_axis); |
|
cJSON_AddItemToObject(AiRadar, "target_y_axis", cJSON_CreateString(c_target_y_axis)); |
|
|
|
char c_distance_meter[20] = {0}; |
|
sprintf(c_distance_meter, "%d", json_distance_meter); |
|
cJSON_AddItemToObject(AiRadar, "distance_meter", cJSON_CreateString(c_distance_meter)); |
|
|
|
char c_distance_meter_y[20] = {0}; |
|
sprintf(c_distance_meter_y, "%d", json_distance_meter_y); |
|
cJSON_AddItemToObject(AiRadar, "distance_meter_y", cJSON_CreateString(c_distance_meter_y)); |
|
|
|
detectArrayRadar = cJSON_CreateArray(); |
|
cJSON_AddItemToObject(AiRadar, "RadarArray", detectArrayRadar); |
|
|
|
for (int i = 0; i < g_current_radar_data_index; i++) |
|
{ |
|
// printf("build_radar_data_json - g_current_radar_data_index : %d, V%ld point\n", i, radar_data_array[i].ttype); |
|
|
|
detectRadar = cJSON_CreateObject(); |
|
cJSON_AddItemToArray(detectArrayRadar, detectRadar); |
|
|
|
char c_tid[10] = {0}; |
|
sprintf(c_tid, "%d", i); |
|
cJSON_AddItemToObject(detectRadar, "tid", cJSON_CreateString(c_tid)); |
|
|
|
char c_ttype[10] = {0}; |
|
sprintf(c_ttype, "%ld", radar_data_array[i].ttype); |
|
cJSON_AddItemToObject(detectRadar, "Type", cJSON_CreateString(c_ttype)); |
|
|
|
if (radar_data_array[i].ttype == 1) |
|
{ |
|
char c_posX[20] = {0}; |
|
sprintf(c_posX, "%f", radar_data_array[i].X); |
|
cJSON_AddItemToObject(detectRadar, "PosX", cJSON_CreateString(c_posX)); |
|
|
|
char c_posY[20] = {0}; |
|
sprintf(c_posY, "%f", radar_data_array[i].Y); |
|
cJSON_AddItemToObject(detectRadar, "PosY", cJSON_CreateString(c_posY)); |
|
|
|
char c_posZ[20] = {0}; |
|
sprintf(c_posZ, "%f", radar_data_array[i].Z); |
|
cJSON_AddItemToObject(detectRadar, "PosZ", cJSON_CreateString(c_posZ)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "velX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "velY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "velZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "ccX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "ccY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "csX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "csY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "tX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "tY", cJSON_CreateString("")); |
|
|
|
char c_doppler[20] = {0}; |
|
sprintf(c_doppler, "%f", radar_data_array[i].doppler); |
|
cJSON_AddItemToObject(detectRadar, "Doppler", cJSON_CreateString(c_doppler)); |
|
|
|
char c_PeakValue[20] = {0}; |
|
sprintf(c_PeakValue, "%f", radar_data_array[i].PeakValue); |
|
cJSON_AddItemToObject(detectRadar, "Peak", cJSON_CreateString(c_PeakValue)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "Speed", cJSON_CreateString("")); |
|
|
|
#if 1 // leo 09-29 |
|
char c_local_radar_x[20] = {0}; |
|
sprintf(c_local_radar_x, "%d", (int)world_radar_data_array[i].X); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_x", cJSON_CreateString(c_local_radar_x)); |
|
|
|
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> x: %d \n", (int)world_radar_data_array[i].X); |
|
|
|
char c_local_radar_y[20] = {0}; |
|
sprintf(c_local_radar_y, "%d", (int)world_radar_data_array[i].Y); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_y", cJSON_CreateString(c_local_radar_y)); |
|
|
|
if (world_radar_data_array[i].X >= 0 && world_radar_data_array[i].X < RADAR_BG_WIDTH) |
|
{ |
|
if (world_radar_data_array[i].Y >= 0 && world_radar_data_array[i].Y < RADAR_BG_HEIGHT) |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("Yes")); |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
#ifdef RADAR_LOG |
|
if (radar_log == 1) |
|
{ // 10-19 leo |
|
// fprintf(fp, "%s,%s,%s,%s,%s,,,,,%s,%s\n", c_framenum, c_ttype, c_posX, c_posY, c_posZ, c_doppler, c_PeakValue); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak |
|
fprintf(fp, "%s,%s,%s,%s,%s,,,,,%s,%s,%d\n", c_framenum, c_ttype, c_posX, c_posY, c_posZ, c_doppler, c_PeakValue, points[i].cluster_id); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak, cluster_id |
|
} |
|
#endif // RADAR_LOG |
|
#endif |
|
// 10-26 pass class_id to client |
|
int class_id = -1; // default : not object |
|
|
|
// 10-25 Ken apply DBSCAN algorithm |
|
if (radar_dbscan == 1) |
|
{ |
|
class_id = rule_based_classifier(points, i, radar_data_array[i].doppler, class_id); |
|
|
|
// printf("V1 DBSCAN - X:%5lf, Y:%5lf, Z:%5lf, cluster_id: %d, class_id: %d\n\n", points[i].x, points[i].y, points[i].z, points[i].cluster_id, class_id); |
|
} |
|
|
|
cJSON_AddItemToObject(detectRadar, "class_id", cJSON_CreateNumber(class_id)); |
|
} |
|
else if (radar_data_array[i].ttype == 2) |
|
{ |
|
char c_posX[20] = {0}; |
|
sprintf(c_posX, "%f", radar_data_array[i].ccX); |
|
cJSON_AddItemToObject(detectRadar, "PosX", cJSON_CreateString(c_posX)); |
|
|
|
char c_posY[20] = {0}; |
|
sprintf(c_posY, "%f", radar_data_array[i].ccY); |
|
cJSON_AddItemToObject(detectRadar, "PosY", cJSON_CreateString(c_posY)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "PosZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "velX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "velY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "velZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "ccX", cJSON_CreateString(c_posX)); |
|
cJSON_AddItemToObject(detectRadar, "ccY", cJSON_CreateString(c_posY)); |
|
|
|
char c_csX[20] = {0}; |
|
sprintf(c_csX, "%f", radar_data_array[i].csX); |
|
cJSON_AddItemToObject(detectRadar, "csX", cJSON_CreateString(c_csX)); |
|
|
|
char c_csY[20] = {0}; |
|
sprintf(c_csY, "%f", radar_data_array[i].csY); |
|
cJSON_AddItemToObject(detectRadar, "csY", cJSON_CreateString(c_csY)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "tX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "tY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "Doppler", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "Peak", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "Speed", cJSON_CreateString("")); |
|
|
|
#if 1 // leo 09-29 |
|
char c_local_radar_x[20] = {0}; |
|
sprintf(c_local_radar_x, "%d", (int)world_radar_data_array[i].ccX); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_x", cJSON_CreateString(c_local_radar_x)); |
|
|
|
char c_local_radar_y[20] = {0}; |
|
sprintf(c_local_radar_y, "%d", (int)world_radar_data_array[i].ccY); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_y", cJSON_CreateString(c_local_radar_y)); |
|
|
|
if (world_radar_data_array[i].ccX >= 0 && world_radar_data_array[i].ccX < RADAR_BG_WIDTH) |
|
{ |
|
if (world_radar_data_array[i].ccY >= 0 && world_radar_data_array[i].ccY < RADAR_BG_HEIGHT) |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("Yes")); |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
#ifdef RADAR_LOG |
|
if (radar_log == 1) |
|
{ // 10-19 leo |
|
// fprintf(fp, "%s,%s,%s,%s,,,,%s,%s,,\n", c_framenum, c_ttype, c_posX, c_posY, c_csX, c_csY); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak |
|
fprintf(fp, "%s,%s,%s,%s,,,,%s,%s,,,\n", c_framenum, c_ttype, c_posX, c_posY, c_csX, c_csY); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak, cluster_id |
|
} |
|
#endif |
|
#endif |
|
cJSON_AddItemToObject(detectRadar, "class_id", cJSON_CreateNumber(-2)); |
|
} |
|
else if (radar_data_array[i].ttype == 3) |
|
{ |
|
char c_posX[20] = {0}; |
|
sprintf(c_posX, "%f", radar_data_array[i].tX); |
|
cJSON_AddItemToObject(detectRadar, "PosX", cJSON_CreateString(c_posX)); |
|
|
|
char c_posY[20] = {0}; |
|
sprintf(c_posY, "%f", radar_data_array[i].tY); |
|
cJSON_AddItemToObject(detectRadar, "PosY", cJSON_CreateString(c_posY)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "PosZ", cJSON_CreateString("")); |
|
|
|
char c_velX[20] = {0}; |
|
sprintf(c_velX, "%f", radar_data_array[i].velX); |
|
cJSON_AddItemToObject(detectRadar, "velX", cJSON_CreateString(c_velX)); |
|
|
|
char c_velY[20] = {0}; |
|
sprintf(c_velY, "%f", radar_data_array[i].velY); |
|
cJSON_AddItemToObject(detectRadar, "velY", cJSON_CreateString(c_velY)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "velZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accY", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "accZ", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "ccX", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "ccY", cJSON_CreateString("")); |
|
|
|
char c_csX[20] = {0}; |
|
sprintf(c_csX, "%f", radar_data_array[i].csX); |
|
cJSON_AddItemToObject(detectRadar, "csX", cJSON_CreateString(c_csX)); |
|
|
|
char c_csY[20] = {0}; |
|
sprintf(c_csY, "%f", radar_data_array[i].csY); |
|
cJSON_AddItemToObject(detectRadar, "csY", cJSON_CreateString(c_csY)); |
|
|
|
char c_tX[20] = {0}; |
|
sprintf(c_tX, "%f", radar_data_array[i].tX); |
|
cJSON_AddItemToObject(detectRadar, "tX", cJSON_CreateString(c_tX)); |
|
|
|
char c_tY[20] = {0}; |
|
sprintf(c_tY, "%f", radar_data_array[i].tY); |
|
cJSON_AddItemToObject(detectRadar, "tY", cJSON_CreateString(c_tY)); |
|
|
|
cJSON_AddItemToObject(detectRadar, "Doppler", cJSON_CreateString("")); |
|
cJSON_AddItemToObject(detectRadar, "Peak", cJSON_CreateString("")); |
|
|
|
char c_Speed[20] = {0}; |
|
sprintf(c_Speed, "%f", radar_data_array[i].Speed); |
|
cJSON_AddItemToObject(detectRadar, "Speed", cJSON_CreateString(c_Speed)); |
|
|
|
#if 1 // leo 09-29 |
|
char c_local_radar_x[20] = {0}; |
|
sprintf(c_local_radar_x, "%d", (int)world_radar_data_array[i].tX); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_x", cJSON_CreateString(c_local_radar_x)); |
|
|
|
char c_local_radar_y[20] = {0}; |
|
sprintf(c_local_radar_y, "%d", (int)world_radar_data_array[i].tY); |
|
cJSON_AddItemToObject(detectRadar, "world_radar_y", cJSON_CreateString(c_local_radar_y)); |
|
|
|
if (world_radar_data_array[i].tX >= 0 && world_radar_data_array[i].tX < RADAR_BG_WIDTH) |
|
{ |
|
if (world_radar_data_array[i].tY >= 0 && world_radar_data_array[i].tY < RADAR_BG_HEIGHT) |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("Yes")); |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
} |
|
else |
|
{ |
|
cJSON_AddItemToObject(detectRadar, "plot", cJSON_CreateString("No")); |
|
} |
|
#ifdef RADAR_LOG |
|
if (radar_log == 1) |
|
{ // 10-19 leo |
|
// fprintf(fp, "%s,%s,%s,%s,,%s,%s,%s,%s,,\n", c_framenum, c_ttype, c_posX, c_posY, c_velX, c_velY, c_csX, c_csY); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak |
|
fprintf(fp, "%s,%s,%s,%s,,%s,%s,%s,%s,,,\n", c_framenum, c_ttype, c_posX, c_posY, c_velX, c_velY, c_csX, c_csY); //"FrameNum, Type, PosX, PosY, PosZ, velX, velY, csX, csY, Doppler, Peak, cluster_id |
|
} |
|
#endif |
|
#endif |
|
cJSON_AddItemToObject(detectRadar, "class_id", cJSON_CreateNumber(-3)); |
|
} |
|
} |
|
#ifdef RADAR_LOG |
|
|
|
if(fp) |
|
{ // 10-19 leo |
|
fclose(fp); |
|
fp = NULL; |
|
} |
|
#endif |
|
char *JsonString = NULL; |
|
|
|
if (root) |
|
{ |
|
JsonString = cJSON_PrintUnformatted(root); |
|
|
|
if (g_switch_radar_data_json == 0) |
|
{ |
|
strcpy(g_radar_data_json, JsonString); |
|
// printf("\nradar_driver - g_radar_data_json:%s\n", g_radar_data_json); |
|
g_switch_radar_data_json = 1; |
|
} |
|
} |
|
|
|
if (JsonString != NULL) |
|
{ |
|
free(JsonString); |
|
JsonString = NULL; |
|
} |
|
|
|
if (root) |
|
{ |
|
cJSON_Delete(root); |
|
root = NULL; |
|
} |
|
|
|
// 10-25 Ken |
|
if (radar_dbscan == 1) |
|
{ |
|
free(points); |
|
radar_dbscan = 0; |
|
} |
|
// printf("---------------- build_radar_data_json end\n"); |
|
} |
|
|
|
int radar_start_status(char *buffer_stream, int buffer_size) |
|
{ |
|
int ret = 0; |
|
if (buffer_size >= LILIN_SHIFT) |
|
{ |
|
if (buffer_stream[0] == 0x01 && |
|
buffer_stream[1] == 0x02 && |
|
buffer_stream[2] == 0x03 && |
|
buffer_stream[3] == 0x04 && |
|
buffer_stream[4] == 0x05 && |
|
buffer_stream[5] == 0x06 && |
|
buffer_stream[6] == 0x07 && |
|
buffer_stream[7] == 0x08) |
|
{ |
|
// printf("magic num: %d-%d-%d-%d-%d-%d-%d-%d \n", buffer_stream[0], buffer_stream[1], buffer_stream[2], buffer_stream[3], buffer_stream[4], buffer_stream[5], buffer_stream[6], buffer_stream[7]); |
|
|
|
if (g_current_radar_data_index >= 1) |
|
{ |
|
build_radar_data_json(); |
|
} |
|
|
|
memset(&radar_service_data, 0x00, sizeof(RadarServiceInfo)); |
|
memset(&radar_data_array, 0x00, MAX_SIZE_RADAR_DATA * sizeof(RadarDataInfo)); |
|
g_current_radar_data_index = 0; |
|
|
|
if (buffer_size == LILIN_SHIFT + MAX_SIZE_LILIN_START * 4) //=40 =header |
|
{ |
|
// printf("\n-------radar start status\n"); |
|
for (int i = 0; i < MAX_SIZE_LILIN_START; i++) |
|
{ |
|
memcpy(union_radar_convert.hexarray, (unsigned char *)buffer_stream + LILIN_SHIFT + i * 4, 4); |
|
if (i == 0) |
|
radar_service_data.version = (long)union_radar_convert.int32; |
|
else if (i == 1) |
|
radar_service_data.totalPacketLen = (long)union_radar_convert.int32; |
|
else if (i == 2) |
|
radar_service_data.platform = (long)union_radar_convert.int32; |
|
else if (i == 3) |
|
radar_service_data.frameNumber = (long)union_radar_convert.int32; |
|
else if (i == 4) |
|
radar_service_data.cpuProcessTime = (long)union_radar_convert.int32; |
|
else if (i == 5) |
|
radar_service_data.numOBJs = (long)union_radar_convert.int32; |
|
else if (i == 6) |
|
radar_service_data.numTLVs = (long)union_radar_convert.int32; |
|
else if (i == 7) |
|
radar_service_data.subFrameNumber = (long)union_radar_convert.int32; |
|
} |
|
|
|
/* |
|
printf("\n----version:%ld\n", radar_service_data.version); |
|
printf("\n----totalPacketLen:%ld\n", radar_service_data.totalPacketLen); |
|
printf("\n----platform:%ld\n", radar_service_data.platform); |
|
printf("\n----frameNumber:%ld\n", radar_service_data.frameNumber); |
|
printf("\n----cpuProcessTime:%ld\n", radar_service_data.cpuProcessTime); |
|
printf("\n----numOBJs:%ld\n", radar_service_data.numOBJs); |
|
printf("\n----numTLVs:%ld\n", radar_service_data.numTLVs); |
|
printf("\n----subFrameNumber:%ld\n", radar_service_data.subFrameNumber);*/ |
|
|
|
// printf("\n----frameNumber:%ld\n", radar_service_data.frameNumber); |
|
// printf("\n----subFrameNumber:%ld\n", radar_service_data.subFrameNumber); |
|
// printf("\n----totalPacketLen:%ld\n", radar_service_data.totalPacketLen); |
|
|
|
// if (radar_service_data.frameNumber - g_old_magic_num > 1) |
|
// printf("lose frame: [%d] -> [%d] \n", g_old_magic_num, radar_service_data.frameNumber); |
|
|
|
g_old_magic_num = radar_service_data.frameNumber; |
|
} |
|
|
|
ret = 1; |
|
} |
|
else |
|
{ |
|
// printf("magic num err: %d-%d-%d-%d-%d-%d-%d-%d \n", |
|
// buffer_stream[0], buffer_stream[1], buffer_stream[2], buffer_stream[3], |
|
// buffer_stream[4], buffer_stream[5], buffer_stream[6], buffer_stream[7]); |
|
} |
|
} |
|
return ret; |
|
} |
|
|
|
void getStream_b(int socketfd, int bSize) |
|
{ |
|
int nbytes; |
|
//char buffer_stream[MAX_IMG_SIZE] = { 0 }; |
|
char buffer[MAX_IMG_SIZE] = { 0 }; |
|
// char boundary[256] = { 0 }; |
|
|
|
//int bodySize = 0; |
|
// int content_length_image = 0; |
|
// int strlen_ptr = 0; |
|
// char *ptr_1, *ptr_2; |
|
|
|
//int ret = 0; |
|
|
|
|
|
//while ((nbytes = recv(socketfd, buffer, bSize, 0)) > 0) |
|
while (bHttpServerMainStart) |
|
{ |
|
nbytes = recv(socketfd, buffer, bSize, 0); |
|
|
|
if (nbytes > 0) |
|
{ |
|
//printf("================================\n"); |
|
//printf("------- recv size = %d \n", nbytes); |
|
//printf("%s \n", buffer); |
|
radar_data_get(buffer, nbytes); |
|
|
|
// if (strstr(buffer, "--myboundary") != NULL) //LILIN package |
|
//{ |
|
// }//recv first package - LILIN package |
|
// else |
|
//{ |
|
// |
|
|
|
// recv sec package - Radar package |
|
#if 0 |
|
if (content_length_image >= nbytes) |
|
{ |
|
if (buffer[0] == 0xf && buffer[1] == 0xf && buffer[2] == 0xf) |
|
printf("----recv end data\n"); |
|
|
|
|
|
memcpy(buffer_stream + bodySize, buffer, content_length_image); |
|
bodySize += nbytes; |
|
content_length_image -= nbytes; |
|
|
|
if (content_length_image <= -1) |
|
{ |
|
//this case should be drop |
|
content_length_image = 0; |
|
} |
|
|
|
if (content_length_image == 0) |
|
{ |
|
ret = Is_radar_data_empty(buffer_stream, bodySize); |
|
if (ret == 0) |
|
{ |
|
//ret = radar_start_status(buffer_stream, bodySize); |
|
//if (ret == 1) |
|
//{ |
|
|
|
//} |
|
|
|
if (radar_start_status(buffer_stream, bodySize) == 1) //radar header |
|
{ |
|
// printf("\n----------decode_stage #1: radar start status\n"); |
|
g_check_if_radar_start = 1; |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
else //radar data |
|
{ |
|
if (g_check_if_radar_start == 1) |
|
{ |
|
if (Is_correct_ttype(buffer_stream, bodySize) == 1) |
|
{ |
|
// printf("\n----------decode_stage #2: find correct ttype\n"); |
|
g_check_if_is_correct_ttype = 1; |
|
} |
|
else if (g_check_if_is_correct_ttype == 1) |
|
{ |
|
|
|
run_general_ttype(buffer_stream, bodySize); //radar_data_array ----> 取得並分析雷達資料 |
|
// printf("\n----------decode_stage #3 #4: run general ttype\n"); |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
else |
|
{ |
|
// printf("\n----------decode_stage #0: incorrect format\n"); |
|
g_check_if_radar_start = 0; |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
} |
|
} |
|
}// if empty |
|
else |
|
{ |
|
//the end of radar data |
|
//reset |
|
|
|
// printf("\n----------decode_stage #0: empty data\n"); |
|
g_check_if_radar_start = 0; |
|
g_check_if_is_correct_ttype = 0; |
|
}// if empty |
|
} |
|
} |
|
else if (nbytes > content_length_image) |
|
{ |
|
//get size error, drop this frame data |
|
//out case: 48 (40+8) |
|
|
|
} |
|
#endif |
|
//}//recv sec package |
|
|
|
// memset(buffer, 0, MAX_IMG_SIZE); |
|
|
|
usSleep(1); |
|
} |
|
} |
|
} |
|
|
|
// static int current_profile_id = 1; |
|
void getStream(int socketfd, int bSize) |
|
{ |
|
|
|
// printf("\n--------socketfd:%d,bSize:%d\n", socketfd, bSize); |
|
char buffer_stream[MAX_IMG_SIZE] = {0}; |
|
char buffer[MAX_IMG_SIZE] = {0}; |
|
size_t bReceived; |
|
|
|
bReceived = recv(socketfd, buffer, bSize, 0); |
|
int bodySize = 0; |
|
int content_length_image = 0; |
|
|
|
/*char *t = */removeHTTPHeader(buffer, &bodySize); |
|
bodySize = 0; |
|
|
|
// if (strstr(buffer, "HTTP/1.1 200") != NULL && strstr(buffer, "encode=jpeg") != NULL && g_framesize_width >= 1 && g_framesize_height >= 1) |
|
{ |
|
// bodySize = 0; |
|
// if (content_length_image >= 1) |
|
{ |
|
memset(buffer, 0, bSize); |
|
bodySize = 0; |
|
content_length_image = 0; |
|
while (bHttpServerMainStart && (bReceived = recv(socketfd, buffer, bSize, 0)) > 0) |
|
{ |
|
|
|
// if (switch_g_image_buff_hd == 1) |
|
{ |
|
memset(buffer_stream, 0x00, MAX_IMG_SIZE); |
|
|
|
// printf("\n-------------------------buffer:%s\n", buffer); |
|
// printf("\n-------------------------bReceived:%d\n", bReceived); |
|
|
|
if (strstr(buffer, "--myboundary") != NULL) |
|
{ |
|
char *ptr_1 = strstr(buffer, "Content-Length: "); |
|
if (ptr_1) |
|
{ |
|
char *ptr_2 = strstr(ptr_1, "Stamp"); |
|
if (ptr_2) |
|
{ |
|
int strlen_ptr = ptr_2 - ptr_1 - 16; //16 = "Content-Length: " |
|
char boundary[256] = {0}; |
|
if (strlen_ptr >= 1) |
|
{ |
|
strncpy(boundary, ptr_1 + 16, strlen_ptr); |
|
// printf("\n-----------boundary:%s\n", boundary); |
|
content_length_image = atoi(boundary); |
|
bodySize = 0; |
|
// printf("\n-------------content_length_image:%d\n", content_length_image); |
|
} |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
if (content_length_image >= 1) |
|
{ |
|
memcpy(buffer_stream + bodySize, buffer, content_length_image); |
|
bodySize += bReceived; |
|
content_length_image -= bReceived; |
|
|
|
if (content_length_image <= -1) |
|
{ |
|
bodySize += content_length_image; |
|
content_length_image = 0; |
|
} |
|
|
|
if (content_length_image == 0) |
|
{ |
|
// printf("\n--------------getStream---TTT------bReceived:%d\n", bReceived); |
|
// printf("\n-------------------buffer_stream#1:%s\n", buffer_stream); |
|
|
|
if (Is_radar_data_empty(buffer_stream, bodySize) == 0) // 0: not empty //data = 0x0f 0x0f 0x0f ..... |
|
{ |
|
|
|
if (radar_start_status(buffer_stream, bodySize) == 1) // is get radar header |
|
{ |
|
// printf("\n----------decode_stage #1: radar start status\n"); |
|
g_check_if_radar_start = 1; |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
else |
|
{ |
|
if (g_check_if_radar_start == 1) |
|
{ |
|
if (Is_correct_ttype(buffer_stream, bodySize) == 1) |
|
{ |
|
//printf("\n----------decode_stage #2: find correct ttype\n"); |
|
g_check_if_is_correct_ttype = 1; // get 8byte data |
|
} |
|
else if (g_check_if_is_correct_ttype == 1) |
|
{ |
|
|
|
run_general_ttype(buffer_stream, bodySize); //radar_data_array ----> 取得並分析雷達資料 |
|
// printf("\n----------decode_stage #3 #4: run general ttype\n"); |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
else |
|
{ |
|
// printf("\n----------decode_stage #0: incorrect format\n"); |
|
g_check_if_radar_start = 0; |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
} |
|
} |
|
} |
|
else |
|
{ |
|
// printf("\n----------decode_stage #0: empty data\n"); |
|
g_check_if_radar_start = 0; |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
|
|
/* |
|
else if (bodySize == LILIN_SHIFT + MAX_SIZE_LILIN_START * 4) { |
|
|
|
|
|
}*/ |
|
|
|
/* |
|
printf("\n---start---\n"); |
|
for (int i = 0; i < bodySize; i++) { |
|
printf("%x,", buffer_stream[i]); |
|
} |
|
printf("\n---end---\n");*/ |
|
} |
|
} |
|
} |
|
} |
|
|
|
memset(buffer, 0, bSize); |
|
usSleep(1); |
|
|
|
/* |
|
if (current_profile_id != (atoi(SystemSetting.getimage_encoder_id) - 1)) |
|
break;*/ |
|
} |
|
} |
|
} |
|
/* |
|
else { |
|
strcpy(g_stream_state_response, "Encoder: The profile name is not jpeg."); |
|
}*/ |
|
|
|
// printf("\n------------bodySize:%d\n", bodySize); |
|
} |
|
|
|
void get_stream_with_device() |
|
{ |
|
g_old_magic_num = 0; |
|
|
|
//int snapshot_size = 0; |
|
|
|
int status; |
|
struct addrinfo host_info; |
|
struct addrinfo *host_info_list = NULL; |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run thread_getstream CCC\n"); |
|
memset(&host_info, 0, sizeof(host_info)); |
|
|
|
host_info.ai_family = AF_INET; // AF_UNSPEC |
|
host_info.ai_socktype = SOCK_STREAM; |
|
host_info.ai_protocol = 0; |
|
|
|
status = getaddrinfo("127.0.0.1", accountData[0].account_port, &host_info, &host_info_list); |
|
if (status != 0) |
|
{ |
|
printf("\ngetaddrinfo error\n"); |
|
strcpy(g_stream_state_response, "Network problem: getaddrinfo error"); |
|
} |
|
else |
|
{ |
|
|
|
int socketfd = -1; |
|
; |
|
// socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype, host_info_list->ai_protocol); |
|
struct timeval timeout; |
|
timeout.tv_sec = 3; // 3 |
|
timeout.tv_usec = 0; |
|
int tcpnodelay = 1; |
|
int reuse = 1; |
|
int qlen = 5; |
|
|
|
/* |
|
struct sockaddr_in info; |
|
bzero(&info, sizeof(info)); |
|
info.sin_family = PF_INET; |
|
|
|
//localhost test |
|
info.sin_addr.s_addr = inet_addr("127.0.0.1"); |
|
info.sin_port = htons(accountData[0].account_port); |
|
//socket的建立 TCP_PORT_IMAGE |
|
socketfd = socket(host_info_list->ai_family, host_info_list->ai_socktype, host_info_list->ai_protocol);*/ |
|
|
|
int err = -1; |
|
|
|
#if 1 |
|
struct addrinfo *rp; |
|
for (rp = host_info_list; rp != NULL; rp = rp->ai_next) |
|
{ |
|
|
|
socketfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol); |
|
if (socketfd == -1) |
|
{ |
|
printf("\nsocket error\n"); |
|
strcpy(g_stream_state_response, "Network problem: socket error"); |
|
} |
|
else |
|
{ |
|
// printf("\n-------open getimage connected (%d)\n", socketfd); |
|
|
|
setsockopt(socketfd, SOL_SOCKET, SO_SNDTIMEO, (const char *)&timeout, sizeof(timeout)); |
|
setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, (const char *)&timeout, sizeof(timeout)); |
|
setsockopt(socketfd, IPPROTO_TCP, TCP_NODELAY, (const char *)&tcpnodelay, sizeof(tcpnodelay)); |
|
setsockopt(socketfd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)); |
|
setsockopt(socketfd, SOL_SOCKET, SO_REUSEPORT, (const char *)&reuse, sizeof(reuse)); |
|
setsockopt(socketfd, SOL_TCP, TCP_FASTOPEN, (const char *)&qlen, sizeof(qlen)); |
|
|
|
if (connect(socketfd, rp->ai_addr, rp->ai_addrlen) != -1) |
|
{ |
|
|
|
for (int i = 0; i < MAX_CLIENT_SOCKET; i++) |
|
{ |
|
if (socketRecords[i].used == 0 && socketRecords[i].sock == -1) |
|
{ |
|
socketRecords[i].sock = socketfd; |
|
|
|
//memset(&(socketRecords[i].recent_sec_data), 0x00, sizeof(SecData)); |
|
//socketRecords[i].thread_id = 0; |
|
socketRecords[i].channel_id = -1; |
|
//socketRecords[i].last_recv_time = time(0); |
|
//socketRecords[i].recent_sec_data[0].recv_time = socketRecords[i].last_recv_time; |
|
socketRecords[i].isGetAlarmMotion = FALSE; |
|
//socketRecords[i].isFirstAlarmMotion = FALSE; |
|
socketRecords[i].isUdpSocket = FALSE; |
|
socketRecords[i].isWebSocketConnect = FALSE; |
|
socketRecords[i].isNvrRequest = 0; |
|
socketRecords[i].isCredentialRequest = 0; |
|
socketRecords[i].debug_type = 3; |
|
#if defined GY_OS_AMBA |
|
//socketRecords[i].iIsAIRelay = 0; |
|
#endif |
|
// printf("\n(%d) [run http server] socketRecords[%d].sock:%d\n", client_sock, i, socketRecords[i].sock); |
|
socketRecords[i].used = 1; |
|
break; |
|
} |
|
else |
|
{ |
|
if (i == MAX_CLIENT_SOCKET - 1) |
|
{ |
|
close(socketfd); |
|
// printf("\n-------close getimage connected (%d)\n", socketfd); |
|
socketfd = -1; |
|
} |
|
} |
|
} |
|
|
|
if (socketfd != -1) |
|
{ |
|
break; |
|
} |
|
} |
|
else |
|
{ |
|
close(socketfd); |
|
// printf("\n-------close getimage connected (%d)\n", socketfd); |
|
socketfd = -1; |
|
} |
|
} |
|
} |
|
#endif |
|
|
|
if (socketfd >= 0) |
|
{ |
|
char Authorization[2560] = {0}; |
|
if (strlen(accountData[0].account_username) > 0) |
|
{ |
|
//strcat(sendBuffer, "Authorization: Basic "); |
|
char buf_account[512] = { 0 }; |
|
urldecode((unsigned char *)accountData[0].account_username, (unsigned char *)buf_account); |
|
strcpy(Authorization, buf_account); |
|
strcat(Authorization, ":"); |
|
|
|
if (strlen(accountData[0].account_password) > 0) { |
|
char buf_account2[512] = { 0 }; |
|
urldecode((unsigned char *)accountData[0].account_password, (unsigned char *)buf_account2); |
|
strcat(Authorization, buf_account2); |
|
} |
|
} |
|
|
|
size_t base64_encode_length = 0; |
|
char httpAuth[2560] = {0}; |
|
if (strlen(Authorization) >= 1) |
|
{ |
|
|
|
base64_encode((const unsigned char *)Authorization, strlen(Authorization), &base64_encode_length, httpAuth); |
|
httpAuth[base64_encode_length] = '\0'; |
|
} |
|
|
|
char msg[2560] = {0}; |
|
|
|
// current_profile_id = i_profileid; |
|
sprintf(msg, "GET /getstream?device=ds164 HTTP/1.1\r\nhost: 127.0.0.1\r\nport: %s\r\nConnection: close\r\n", accountData[0].account_port); |
|
|
|
strcat(msg, "Authorization: Basic "); |
|
strcat(msg, httpAuth); |
|
strcat(msg, "\r\n"); |
|
strcat(msg, "\r\n"); |
|
|
|
// printf("\n-----------msg:%s\n", msg); |
|
|
|
err = send(socketfd, msg, strlen(msg), 0); |
|
if (err == -1) |
|
{ |
|
printf("\nerror sending\n"); |
|
strcpy(g_stream_state_response, "Network problem: error sending"); |
|
|
|
// exit(EXIT_FAILURE); |
|
} |
|
else |
|
{ |
|
init_radar_parser(); |
|
init_axis_covert_world(); |
|
// getStream(socketfd, MAX_IMG_SIZE); |
|
getStream_b(socketfd, MAX_IMG_SIZE); |
|
} |
|
|
|
close(socketfd); |
|
return; |
|
// printf("\n-------close getimage connected (%d)\n", socketfd); |
|
|
|
for (int i = 0; i < MAX_CLIENT_SOCKET; i++) |
|
{ |
|
if (socketRecords[i].used == 1 && socketRecords[i].sock == socketfd) |
|
{ |
|
// printf("\n\n(%d) [http connection fun] clean socket[%d]\n\n", sock, i); |
|
|
|
socketRecords[i].sock = -1; |
|
//socketRecords[i].thread_id = 0; |
|
socketRecords[i].channel_id = -1; |
|
//socketRecords[i].last_recv_time = time(0); |
|
//socketRecords[i].recent_sec_data[0].recv_time = socketRecords[i].last_recv_time; |
|
socketRecords[i].isGetAlarmMotion = FALSE; |
|
//socketRecords[i].isFirstAlarmMotion = FALSE; |
|
socketRecords[i].isWebSocketConnect = FALSE; |
|
socketRecords[i].isUdpSocket = FALSE; |
|
socketRecords[i].isNvrRequest = 0; |
|
socketRecords[i].isCredentialRequest = 0; |
|
socketRecords[i].debug_type = 0; |
|
#if defined GY_OS_AMBA |
|
//socketRecords[i].iIsAIRelay = 0; |
|
#endif |
|
|
|
socketRecords[i].used = 0; |
|
break; |
|
} |
|
} |
|
|
|
socketfd = -1; |
|
} |
|
else |
|
{ |
|
printf("\nCould not create socket!\n"); |
|
strcpy(g_stream_state_response, "Network problem: Could not create socket!"); |
|
} |
|
} |
|
|
|
if (host_info_list != NULL) |
|
{ |
|
freeaddrinfo(host_info_list); |
|
host_info_list = NULL; |
|
} |
|
} |
|
|
|
#if 0 |
|
void radar_data_parser() |
|
{ |
|
int iBlockDataSize; |
|
char *sBlockDataBuffer; |
|
int iCurrentRead = 0; |
|
|
|
while (1) |
|
{ |
|
if (RadarQueueSize() > 0) |
|
{ |
|
//printf("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< queue size: %d \n", RadarQueueSize()); |
|
|
|
RadarFrameData qPopRadarData = { 0 }; |
|
|
|
qPopRadarData = RadarQueueFront(); |
|
RadarQueuePop(); |
|
|
|
iCurrentRead = 0; |
|
iBlockDataSize = 0; |
|
|
|
//radar_start_status |
|
//printf("<<<<<<<<<<<< pop queue, data size = %d, blocknum: %d \n", qPopRadarData.iDataSize, qPopRadarData.iDataBlockNumber); |
|
|
|
if (qPopRadarData.iDataBlockNumber > 0) |
|
{ |
|
for (int i = 0; i < qPopRadarData.iDataBlockNumber; i++) |
|
{ |
|
iBlockDataSize = qPopRadarData.iDataBlockArray[i]; |
|
//printf("-%d ", qPopRadarData.iDataBlockArray[i]); |
|
|
|
if (i == 0 && iBlockDataSize != 40) |
|
{ |
|
break; |
|
} |
|
else if (i == 0 && iBlockDataSize == 40) |
|
{ |
|
iCurrentRead = 40; |
|
continue; |
|
} |
|
|
|
sBlockDataBuffer = qPopRadarData.frame_buffer + iCurrentRead; |
|
iCurrentRead += iBlockDataSize; |
|
printf("this correct_ttype a: %d \n", iBlockDataSize); |
|
if (Is_correct_ttype(sBlockDataBuffer, iBlockDataSize) == 1) |
|
{ |
|
// printf("\n----------decode_stage #2: find correct ttype\n"); |
|
printf("this correct_ttype b: %d \n", iBlockDataSize); |
|
g_check_if_is_correct_ttype = 1; //get 8byte data |
|
} |
|
else if (g_check_if_is_correct_ttype == 1) |
|
{ |
|
run_general_ttype(sBlockDataBuffer, iBlockDataSize); //radar_data_array_p ----> 取得並分析雷達資料 |
|
g_check_if_is_correct_ttype = 0; |
|
} |
|
} |
|
//printf("\n"); |
|
} |
|
|
|
//printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> %d \n", g_current_radar_data_index); |
|
|
|
if (g_current_radar_data_index >= 1) |
|
{ |
|
build_radar_data_json(); |
|
} |
|
|
|
memset(&radar_service_data, 0x00, sizeof(RadarServiceInfo)); |
|
memset(&radar_data_array, 0x00, MAX_SIZE_RADAR_DATA * sizeof(RadarDataInfo)); |
|
g_current_radar_data_index = 0; |
|
} |
|
|
|
usSleep(10); |
|
} |
|
} |
|
#endif |
|
|
|
void *thread_getstream(void *ptr) |
|
{ |
|
pthread_detach(pthread_self()); |
|
// update_radar_json(); // leo 09-29 |
|
update_radar_json_p(); // leo 11-25 |
|
// while (get_bHttpServerThreadStart()) |
|
//{ |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run thread_getstream\n"); |
|
get_stream_with_device(); |
|
usSleep(80000); |
|
//} |
|
pthread_exit(NULL); |
|
} |
|
|
|
void *thread_parse_radar(void *ptr) |
|
{ |
|
pthread_detach(pthread_self()); |
|
|
|
// while (get_bHttpServerThreadStart()) |
|
// while (1) |
|
//{ |
|
printf(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> run thread radar parser\n"); |
|
radar_data_parser(); |
|
|
|
// usSleep(80000); |
|
//} |
|
|
|
pthread_exit(NULL); |
|
}
|
|
|