Release Tizen2.0 beta
[framework/location/gps-manager.git] / gps-manager / nmea_logger.c
1 /*
2  * gps-manager
3  *
4  * Copyright (c) 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Youngae Kang <youngae.kang@samsung.com>, Yunhan Kim <yhan.kim@samsung.com>,
7  *          Genie Kim <daejins.kim@samsung.com>, Minjune Kim <sena06.kim@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include "nmea_logger.h"
31 #include "debug_util.h"
32
33 #define MAX_NMEA_RAW_DATA_LOG_FILE_CNT  (999)
34 #define MAX_NMEA_LOG_FILE_PATH          (100)
35
36 #define PHONE_FOLDER                    "/opt/media"
37 #define GPSMANAGER_FOLDER               PHONE_FOLDER"/gps-manager"
38 #define NMEA_FOLDER                     GPSMANAGER_FOLDER"/NMEA"
39 #define NMEA_LOGGING_FILE_PATH          NMEA_FOLDER"/nmea_data"
40
41 int raw_nmea_fd = -1;
42
43 static int generate_nmea_log_file(char *);
44
45 void start_nmea_log()
46 {
47         char filepath[MAX_NMEA_LOG_FILE_PATH];
48
49         // File Open
50         struct stat st = {0};
51
52         if (stat(GPSMANAGER_FOLDER, &st) == -1) {
53             mkdir(GPSMANAGER_FOLDER, 0777);
54         }
55
56         if(stat(NMEA_FOLDER, &st) == -1)
57                 mkdir(NMEA_FOLDER, 0777);
58
59
60         if (generate_nmea_log_file(filepath) == -1) {
61                 LOG_GPS(DBG_ERR, "Starting LBS Logging for RAW NMEA data FAILED!");
62                 raw_nmea_fd = -1;
63                 return;
64         }
65
66         raw_nmea_fd = open(filepath, O_RDWR | O_APPEND | O_CREAT, 0644);
67
68         if (raw_nmea_fd < 0) {
69                 LOG_GPS(DBG_ERR, "FAILED to open [%s], error[%d]", filepath, errno);
70         } else {
71                 LOG_GPS(DBG_LOW, "Success :: %s raw_nmea_fd [%d]", filepath, raw_nmea_fd);
72         }
73
74         return;
75 }
76
77 void stop_nmea_log()
78 {
79         int close_ret_val = 0;
80
81         LOG_GPS(DBG_LOW, "raw_nmea_fd [%d]", raw_nmea_fd);
82
83         if (raw_nmea_fd != -1) {
84                 close_ret_val = close(raw_nmea_fd);
85                 if (close_ret_val < 0) {
86                         LOG_GPS(DBG_ERR, "FAILED to close raw_nmea_fd[%d], error[%d]", raw_nmea_fd, errno);
87                 }
88                 raw_nmea_fd = -1;
89         }
90         return;
91 }
92
93 void write_nmea_log(char *data, int len)
94 {
95         int write_ret_val = 0;
96
97         if (raw_nmea_fd != -1) {
98                 write_ret_val = write(raw_nmea_fd, data, len);
99                 if (write_ret_val < 0) {
100                         LOG_GPS(DBG_ERR, "FAILED to write[%d], error[%d]", raw_nmea_fd, errno);
101                         close(raw_nmea_fd);
102                         raw_nmea_fd = -1;
103                 }
104         }
105         return;
106 }
107
108 static int generate_nmea_log_file(char *filepath)
109 {
110         int idx = 0;
111         int fd = 0;
112         char fn[MAX_NMEA_LOG_FILE_PATH];
113
114         for (idx = 0; idx < MAX_NMEA_RAW_DATA_LOG_FILE_CNT; idx++) {
115                 snprintf(fn, MAX_NMEA_LOG_FILE_PATH, "%s%03d.txt", NMEA_LOGGING_FILE_PATH, idx);
116                 if ((fd = access(fn, R_OK)) == -1) {
117                         LOG_GPS(DBG_LOW, "Next log file [%s]", fn);
118                         strcpy(filepath, fn);
119                         return 0;
120                 }
121         }
122         LOG_GPS(DBG_LOW, "All NMEA RAW Data logging files are used. New log file can not be created");
123         return -1;
124 }