aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Embedded_SW/Embedded/Common/SWUpdate/FileSystem.c
blob: c1f6737457b61afdc597860c03acf94d08e0b123 (plain)
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using Tango.MachineStudio.Common;
using Tango.SharedUI;

namespace Tango.MachineStudio.UI.ViewModels
{
    public class ModuleWindowVM : ViewModel
    {
        private IStudioModule _module;

        public IStudioModule Module
        {
            get { return _module; }
            set { _module = value; RaisePropertyChangedAuto(); }
        }

        public ModuleWindowVM(IStudioModule module)
        {
            Module = module;
        }
    }
}
d='n310' href='#n310'>310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
/*
 * FileSystem.c
 *
 *  Created on: Oct 23, 2018
 *      Author: shlomo
 */
#include "include.h"
#include "Communication/CommunicationTask.h"
#include <DataDef.h>

#include  <Communication/PMR/IO/FileUploadRequest.pb-c.h>
#include  <Communication/PMR/IO/FileUploadResponse.pb-c.h>
#include  <Communication/PMR/IO/FileChunkUploadRequest.pb-c.h>
#include  <Communication/PMR/IO/FileChunkUploadResponse.pb-c.h>
#include  <Communication/PMR/IO/ExecuteProcessRequest.pb-c.h>
#include  <Communication/PMR/IO/ExecuteProcessResponse.pb-c.h>
#include  <Communication/PMR/IO/KillProcessRequest.pb-c.h>
#include  <Communication/PMR/IO/KillProcessResponse.pb-c.h>
#include  <Communication/PMR/IO/CreateRequest.pb-c.h>
#include  <Communication/PMR/IO/CreateResponse.pb-c.h>
#include  <Communication/PMR/IO/DeleteRequest.pb-c.h>
#include  <Communication/PMR/IO/DeleteResponse.pb-c.h>
#include  <Communication/PMR/IO/GetStorageInfoRequest.pb-c.h>
#include  <Communication/PMR/IO/GetStorageInfoResponse.pb-c.h>
#include  <Communication/PMR/IO/GetFilesRequest.pb-c.h>
#include  <Communication/PMR/IO/GetFilesResponse.pb-c.h>
#include  <Communication/PMR/IO/FileAttribute.pb-c.h>

#include "third_party/fatfs/src/ffconf.h"
#include "third_party/fatfs/src/diskio.h"
#include "drivers/Flash_Memory/Flash_Memory.h"
#include "drivers/Flash_Memory/fatfs/ff.h"

#include "third_party/fatfs/src/ffconf.h"
FIL *FileHandle = 0;  //the system supports a single active file
char FileHandleChar[5];
char ErrorMsg[100];
#define MAX_CHUNK_LENGTH 2000
int FileLength = 0;
int FileReceivedLength = 0;
static char g_cCwdBuf[50] = "/";
uint32_t WrittenBytes = 0;

ErrorCode FileError_to_ErrorCode[FR_INVALID_PARAMETER+1] = {ERROR_CODE__NONE,ERROR_CODE__FILE_REQUEST_DISK_ERR,ERROR_CODE__FILE_REQUEST_INT_ERR,ERROR_CODE__FILE_REQUEST_NOT_READY,ERROR_CODE__FILE_REQUEST_NO_FILE,
                                                       ERROR_CODE__FILE_REQUEST_NO_PATH,ERROR_CODE__FILE_REQUEST_INVALID_NAME,ERROR_CODE__FILE_REQUEST_DENIED,ERROR_CODE__FILE_REQUEST_EXIST,ERROR_CODE__FILE_REQUEST_INVALID_OBJECT,
                                                       ERROR_CODE__FILE_REQUEST_WRITE_PROTECTED,ERROR_CODE__FILE_REQUEST_INVALID_DRIVE,ERROR_CODE__FILE_REQUEST_NOT_ENABLED,ERROR_CODE__FILE_REQUEST_NO_FILESYSTEM,
                                                       ERROR_CODE__FILE_REQUEST_MKFS_ABORTED,ERROR_CODE__FILE_REQUEST_TIMEOUT,ERROR_CODE__FILE_REQUEST_LOCKED,ERROR_CODE__FILE_REQUEST_NOT_ENOUGH_CORE,ERROR_CODE__FILE_REQUEST_TOO_MANY_OPEN_FILES,
                                                       ERROR_CODE__FILE_REQUEST_INVALID_PARAMETER};


ErrorCode getErrorCode(FRESULT Fresult)
{
    return  FileError_to_ErrorCode[Fresult];
}
bool isDirectory(FileAttribute FileAtt)
{
    if (FileAtt&&FILE_ATTRIBUTE__Directory)
        return true;
    return false;
}

uint32_t    FileUploadRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    FRESULT Fresult = FR_OK;

    MessageContainer responseContainer;

    FileUploadRequest* request = file_upload_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    FileUploadResponse response = FILE_UPLOAD_RESPONSE__INIT;

    WrittenBytes=0;
    FileHandle = malloc(sizeof(FIL));
    if (FileHandle == 0)
        Fresult = FR_DENIED;
    else
    {
        Fresult = f_open(FileHandle,request->path,FA_READ | FA_WRITE |  FA_OPEN_ALWAYS );
        if (Fresult == FR_OK)
        {
            FileLength = request->length;
            response.has_maxchunklength = true;
            response.maxchunklength = MAX_CHUNK_LENGTH;
            strcpy(FileHandleChar, "1234");
            response.uploadid = FileHandleChar; //supporting only single file at each time.
        }

    }


    responseContainer = createContainer(MESSAGE_TYPE__FileUploadResponse,  requestContainer->token, false, &response, &file_upload_response__pack, &file_upload_response__get_packed_size);
    if (Fresult!= FR_OK)
    {
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    file_upload_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}

uint32_t    FileChunkUploadRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;
    FRESULT Fresult = FR_OK;


    MessageContainer responseContainer;

    FileChunkUploadRequest* request = file_chunk_upload_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    FileChunkUploadResponse response = FILE_CHUNK_UPLOAD_RESPONSE__INIT;

    FIL *ReceivedFileHandle;  //the system supports a single active file

  //  if (request->uploadid == 1)
    ReceivedFileHandle = FileHandle;
//    memcpy (&ReceivedFileHandle,request->uploadid,sizeof(ReceivedFileHandle));
    //if (ReceivedFileHandle == FileHandle)
    //{
        Fresult = f_write(ReceivedFileHandle,request->buffer.data,request->buffer.len,&WrittenBytes );
        if(Fresult != FR_OK)
        {
            LOG_ERROR (Fresult,"f_write error");
        }
        else
        {
            FileReceivedLength += WrittenBytes;
            if (FileReceivedLength == FileLength)
            {
                REPORT_MSG (FileReceivedLength,"file upload ended successfully");
                f_close(ReceivedFileHandle);
                free (FileHandle);
            }
            else
            {
                if (FileReceivedLength > FileLength)
                {
                    REPORT_MSG (FileReceivedLength,"file upload too much data!");
                    f_close(ReceivedFileHandle);
                    free (FileHandle);
                }
            }
        }
    /*}
    else
    {
        LOG_ERROR (ReceivedFileHandle,"file id error");
        f_close(FileHandle);
        free (FileHandle);
        status = ERROR_CODE__FILE_NOT_FOUND;
    }*/

    responseContainer = createContainer(MESSAGE_TYPE__FileChunkUploadResponse,  requestContainer->token, false, &response, &file_chunk_upload_response__pack, &file_chunk_upload_response__get_packed_size);
    if (Fresult!= OK)
    {
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    file_chunk_upload_request__free_unpacked(request,NULL);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    ExecuteProcessRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    ExecuteProcessRequest* request = execute_process_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    ExecuteProcessResponse response = EXECUTE_PROCESS_RESPONSE__INIT;


    responseContainer = createContainer(MESSAGE_TYPE__ExecuteProcessResponse,  requestContainer->token, false, &response, &execute_process_response__pack, &execute_process_response__get_packed_size);
    if (status!= OK)
    {
        responseContainer.error = ERROR_CODE__GENERAL_ERROR;//getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    execute_process_request__free_unpacked(request,NULL);
    my_free(responseContainer.data.data);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    KillProcessRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    KillProcessRequest* request = kill_process_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    KillProcessResponse response = KILL_PROCESS_RESPONSE__INIT;


    responseContainer = createContainer(MESSAGE_TYPE__KillProcessResponse,  requestContainer->token, false, &response, &kill_process_response__pack, &kill_process_response__get_packed_size);
    if (status!= OK)
    {
        responseContainer.error = ERROR_CODE__GENERAL_ERROR;//getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    kill_process_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    CreateRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;
    FRESULT Fresult = FR_OK;
    MessageContainer responseContainer;

    CreateRequest* request = create_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    CreateResponse response = CREATE_RESPONSE__INIT;

    if (isDirectory(request->attribute))
    {
        Fresult = f_mkdir(request->path);
    }
    else
    {
        FileHandle = malloc(sizeof(FIL));
        if (FileHandle == 0)
            Fresult = FR_DENIED;
        else
        {
            Fresult = f_open(FileHandle,request->path,FA_CREATE_NEW);
            if (Fresult == FR_OK)
            {
                if (f_close (FileHandle)!= FR_OK)
                {
                    Fresult = FR_LOCKED;
                }
            }
        }
    }
    if (Fresult != FR_OK)
    {
        status = ERROR_CODE__FILE_NOT_FOUND;
        usnprintf(ErrorMsg, 100, "File Operation failed error code %d",Fresult);

    }
    free (FileHandle);

    responseContainer = createContainer(MESSAGE_TYPE__CreateResponse,  requestContainer->token, false, &response, &create_response__pack, &create_response__get_packed_size);
    if (Fresult!= OK)
    {
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    create_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    DeleteRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    DeleteRequest* request = delete_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    DeleteResponse response = DELETE_RESPONSE__INIT;

    DIR dir;
    FILINFO* fno = 0;
    int NumOfFiles = 0;
    FRESULT Fresult = FR_OK;

    Fresult |= f_opendir(&dir, g_cCwdBuf);
    if(Fresult != FR_OK)
    {
        LOG_ERROR (Fresult,"f_write error");
    }
    else
    {

        fno = my_malloc(sizeof(FILINFO));
        memset (fno,0,sizeof(FILINFO));
        Fresult = f_stat(request->path,fno);
        if (Fresult == FR_OK)
        {
            if (isDirectory(fno->fattrib))
            {
                //============================
                Fresult = f_opendir(&dir, request->path);                       /* Open the directory */
                 if (Fresult == FR_OK)
                 {
                     Fresult = f_readdir(&dir, &fno);                   /* Read a directory item */
                     if (Fresult == FR_OK)
                     {
                         if(fno->fname[0] ==0)
                         {
                             //no files
                             Fresult = f_unlink(request->path);
                         }
                         else
                         {
                             LOG_ERROR (fno,"Directory not empty");
                         }
                     }
                 }
            }
            else
            {
                Fresult = f_unlink(request->path);
            }
        }
    }
    responseContainer = createContainer(MESSAGE_TYPE__DeleteResponse,  requestContainer->token, false, &response, &delete_response__pack, &delete_response__get_packed_size);
    if (Fresult!= OK)
    {
        usnprintf(ErrorMsg, 100, "File Operation failed error code %d",Fresult);
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = ErrorMsg;
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    my_free(fno);
    delete_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    GetStorageInfoRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    GetStorageInfoRequest* request = get_storage_info_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    GetStorageInfoResponse response = GET_STORAGE_INFO_RESPONSE__INIT;
    FATFS *fs;
    DWORD fre_clust, fre_sect, tot_sect;
    FRESULT Fresult = FR_OK;


    /* Get volume information and free clusters of drive 1 */
    Fresult = f_getfree("0:", &fre_clust, &fs);
     if (Fresult == FR_OK)
     {

    /* Get total sectors and free sectors */
         tot_sect = (fs->n_fatent - 2) * fs->csize;
         fre_sect = fre_clust * fs->csize;
         response.has_capacity = true;
         response.capacity = tot_sect/2;
         response.has_freespace = true;
         response.freespace = fre_sect/2;
     }


    responseContainer = createContainer(MESSAGE_TYPE__GetStorageInfoResponse,  requestContainer->token, false, &response, &get_storage_info_response__pack, &get_storage_info_response__get_packed_size);
    if (Fresult!= OK)
    {
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    get_storage_info_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);

    return OK;
}
uint32_t    GetFilesRequestFunc(MessageContainer* requestContainer)
{
    uint32_t status = OK;

    MessageContainer responseContainer;

    GetFilesRequest* request = get_files_request__unpack(NULL, requestContainer->data.len, requestContainer->data.data);

    GetFilesResponse response = GET_FILES_RESPONSE__INIT;

    #define MAX_NUM_OF_FILES 10
    DIR dir;
    FILINFO* fno[MAX_NUM_OF_FILES];
    int i,NumOfFiles = 0;
    FRESULT Fresult = FR_OK;

    Fresult |= f_opendir(&dir, g_cCwdBuf);
    if(Fresult != FR_OK)
    {
        return(Fresult);
    }

    memset (fno,0,sizeof(fno));
    FileInfo **FilesInfo = (FileInfo**)my_malloc(sizeof(FileInfo *)*(MAX_NUM_OF_FILES));
    FileInfo Data[MAX_NUM_OF_FILES];
    memset(fno,0,sizeof(fno));
    fno[0] = my_malloc(sizeof(FILINFO));
	memset (fno[0],0,sizeof(FILINFO));

    Fresult = f_stat(request->path,fno[0]);
    if (Fresult == FR_OK)
    {
        if (isDirectory(fno[0]->fattrib))
        {
            NumOfFiles++;
            //============================
            Fresult = f_opendir(&dir, request->path);                       /* Open the directory */
             if (Fresult == FR_OK)
             {
                 for (NumOfFiles = 1;NumOfFiles < MAX_NUM_OF_FILES;NumOfFiles++)
                 {
                     fno[NumOfFiles] = my_malloc(sizeof(FILINFO));
				     memset (fno[NumOfFiles],0,sizeof(FILINFO));
                     Fresult = f_readdir(&dir, fno[NumOfFiles]);                   /* Read a directory item */
                     if (Fresult == FR_OK)
                     {
                         if(fno[NumOfFiles]->fname[0] ==0)
                         {
                             break;
                         }
                     }
                 }
             }
        }
        else
        {
            Fresult = FR_DENIED;
        }
    }

    if ((Fresult == FR_OK)&&(NumOfFiles))
    {
        for (i = 0;i < NumOfFiles;i++)
        {
           file_info__init(&Data[i]);
           FilesInfo[i] = &Data[i];
           Data[i].has_attribute = true;
           Data[i].attribute = fno[i]->fattrib;
           Data[i].has_length = true;
           Data[i].name = fno[i]->fname;
           Data[i].has_length = true;
           Data[i].length = fno[i]->fsize;
           Data[i].lastmodifieddate = fno[i]->fdate;
           Data[i].lastmodifiedtime = fno[i]->ftime;
        }
        response.n_items = NumOfFiles;
        response.items = FilesInfo;
    }
    else
    {
        response.n_items = 0;
        response.items = NULL;
    }
    responseContainer = createContainer(MESSAGE_TYPE__GetFilesResponse,  requestContainer->token, false, &response, &get_files_response__pack, &get_files_response__get_packed_size);

    if (Fresult!= OK)
    {
        responseContainer.error = getErrorCode(Fresult);
        responseContainer.errormessage = "File operation error";
    }
    responseContainer.continuous = false;
    uint8_t* container_buffer = my_malloc(message_container__get_packed_size(&responseContainer));
    size_t container_size = message_container__pack(&responseContainer, container_buffer);
    my_free(responseContainer.data.data);
    my_free(FilesInfo);
    get_files_request__free_unpacked(request,NULL);
    SendChars(container_buffer, container_size);
    for (i = 0;i < NumOfFiles;i++)
    {
        if (fno[i])
               my_free (fno[i]);
    }
    return Fresult;
}